Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions java/0721-accounts-merge. java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class Solution {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
int n = accounts.size();
DSU dsu = new DSU(n);

Map<String, Integer> map = new HashMap<>(); // email -> index of acc

for(int i = 0; i < n; i++){
for(int j = 1; j < accounts.get(i).size(); j++){
String email = accounts.get(i).get(j);
String name = accounts.get(i).get(0);

if(!map.containsKey(email))
map.put(email, i);
else
dsu.union(i, map.get(email));
}
}

Map<Integer, List<String>> merged = new HashMap<>(); // index of acc -> list of emails
for(String email : map.keySet()){
int group = map.get(email);
int lead = dsu.find(group);

if(!merged.containsKey(lead))
merged.put(lead, new ArrayList<String>());

merged.get(lead).add(email);
}

List<List<String>> res = new ArrayList<>();
for(int ac : merged.keySet()){
List<String> grp = merged.get(ac);
Collections.sort(grp);
grp.add(0, accounts.get(ac).get(0));
res.add(grp);
}
return res;
}
}

class DSU {
int[] parent;
int[] rank;

public DSU(int size) {
rank = new int[size];
parent = new int[size];
for (int i = 0; i < size; i++)
parent[i] = i;
}

public int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
}

// Union By Rank

public boolean union(int x, int y) {
int xr = find(x), yr = find(y);
if (xr == yr) {
return false;
} else if (rank[xr] < rank[yr]) {
parent[xr] = yr;
} else if (rank[xr] > rank[yr]) {
parent[yr] = xr;
} else {
parent[yr] = xr;
rank[xr]++;
}
return true;
}
}
25 changes: 25 additions & 0 deletions java/0802-find-eventual-safe-states.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public List<Integer> eventualSafeNodes(int[][] graph) {
List<Integer> res = new ArrayList<>();
HashMap<Integer, Boolean> safe = new HashMap<>();
for(int i = 0; i < graph.length; i++){
if(dfs(graph, i, safe))
res.add(i);
}
return res;
}

private boolean dfs(int[][] graph, int src, HashMap<Integer, Boolean> safe) {
if (safe.containsKey(src))
return safe.get(src);

safe.put(src, false);

for (int neighbour : graph[src]) {
if (!dfs(graph, neighbour, safe))
return false;
}
safe.put(src, true);
return true;
}
}
29 changes: 29 additions & 0 deletions java/2477-minimum-fuel-cost-to-report-to-the-capital.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
long res = 0;
HashMap<Integer, List<Integer>> g = new HashMap<>();
public long minimumFuelCost(int[][] roads, int seats) {
for(int[] e : roads){
int a = e[0], b = e[1];
g.computeIfAbsent(a, val -> new ArrayList<>()).add(b);
g.computeIfAbsent(b, val -> new ArrayList<>()).add(a);
}
if (g.size() == 0)
return 0;

dfs(0, -1, seats);
return res;
}

private int dfs(int node, int parent, int seats){
int passengers = 0;

for(int child : g.get(node)){
if(child != parent){
int p = dfs(child, node, seats);
passengers += p;
res += (int) Math.ceil((double)p/seats);
}
}
return passengers + 1;
}
}