-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc6ce22
commit 6b39ccb
Showing
4 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for C++ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
// } Driver Code Ends | ||
//User function Template for C++ | ||
|
||
class DisjointSet | ||
{ | ||
public: | ||
vector<int> root, rank; | ||
|
||
DisjointSet(int n) | ||
{ | ||
root.resize(n + 1); | ||
rank.resize(n + 1); | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
root[i] = i; | ||
rank[i] = 1; | ||
} | ||
} | ||
|
||
int findUP(int node) | ||
{ | ||
if (node == root[node]) | ||
{ | ||
return node; | ||
} | ||
return root[node] = findUP(root[node]); | ||
} | ||
|
||
void unionBySize(int u, int v) | ||
{ | ||
int root_u = findUP(u); | ||
int root_v = findUP(v); | ||
if (rank[root_u] < rank[root_v]) | ||
{ | ||
root[root_u] = root_v; | ||
rank[root_v] += rank[root_u]; | ||
} | ||
else | ||
{ | ||
root[root_v] = root_u; | ||
rank[root_u] += rank[root_v]; | ||
} | ||
return; | ||
} | ||
}; | ||
|
||
class Solution | ||
{ | ||
public: | ||
vector<vector<string>> accountsMerge(vector<vector<string>> &accounts) | ||
{ | ||
int n = accounts.size(); | ||
DisjointSet ds(n); | ||
vector<string> consolidated[n]; | ||
unordered_map<string, int> email_to_index; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
for (int j = 1; j < accounts[i].size(); j++) | ||
{ | ||
string email = accounts[i][j]; | ||
if (email_to_index.find(email) == email_to_index.end()) | ||
{ | ||
email_to_index[email] = i; | ||
} | ||
else | ||
{ | ||
ds.unionBySize(i, email_to_index[email]); | ||
} | ||
} | ||
} | ||
|
||
for (auto it : email_to_index) | ||
{ | ||
string email = it.first; | ||
int parentIndex = ds.findUP(it.second); | ||
consolidated[parentIndex].push_back(email); | ||
} | ||
|
||
vector<vector<string>> result; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
if (consolidated[i].empty()) | ||
continue; | ||
sort(consolidated[i].begin(), consolidated[i].end()); | ||
vector<string> temp; | ||
temp.push_back(accounts[i][0]); | ||
for (auto &email : consolidated[i]) | ||
{ | ||
temp.push_back(email); | ||
} | ||
result.push_back(temp); | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
|
||
|
||
//{ Driver Code Starts. | ||
int main() | ||
{ | ||
int t; | ||
cin >> t; | ||
while (t--) | ||
{ | ||
int n; | ||
cin >> n; | ||
vector<vector<string>> accounts; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
vector<string> temp; | ||
int x; | ||
cin >> x; | ||
|
||
for (int j = 0; j < x; j++) | ||
{ | ||
string s1; | ||
cin >> s1; | ||
temp.push_back(s1); | ||
} | ||
accounts.push_back(temp); | ||
} | ||
|
||
Solution obj; | ||
vector<vector<string>> res = obj.accountsMerge(accounts); | ||
sort(res.begin(), res.end()); | ||
cout << "["; | ||
for (int i = 0; i < res.size(); ++i) | ||
{ | ||
cout << "["; | ||
for (int j = 0; j < res[i].size(); j++) | ||
{ | ||
if (j != res[i].size() - 1) | ||
cout << res[i][j] << "," | ||
<< " "; | ||
else | ||
cout << res[i][j]; | ||
} | ||
if (i != res.size() - 1) | ||
cout << "], " << endl; | ||
else | ||
cout << "]"; | ||
} | ||
cout << "]" | ||
<< endl; | ||
} | ||
} | ||
// } Driver Code Ends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n*m*logn) | ||
Space complexity - O(m*n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(m*n*log(m*n)) | ||
Space complexity - O(m*n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
class Solution | ||
{ | ||
public int maximumSafenessFactor(List<List<Integer>> grid) | ||
{ | ||
PriorityQueue<int[]> pq = new PriorityQueue<>((x,y) -> y[2]-x[2]); | ||
Queue<int[]> q = new LinkedList(); | ||
|
||
int m = grid.size(); | ||
int n = grid.get(0).size(); | ||
|
||
int[][] dir = {{1,0},{0,1},{-1,0},{0,-1}}; | ||
|
||
if (grid.get(0).get(0)==1 || grid.get(m-1).get(n-1)==1) | ||
return 0; | ||
|
||
int step[][] = new int[m][n]; | ||
|
||
for (int i=0; i<m; i++) | ||
{ | ||
for (int j=0; j<n; j++) | ||
{ | ||
if (grid.get(i).get(j)==1) | ||
{ | ||
q.offer(new int[]{i, j}); | ||
step[i][j] = 1; | ||
} | ||
} | ||
} | ||
|
||
int steps = 1; | ||
|
||
while (!q.isEmpty()) | ||
{ | ||
steps++; | ||
int size = q.size(); | ||
for (int s=0; s<size; s++) | ||
{ | ||
int cur[] = q.poll(); | ||
int x = cur[0]; | ||
int y = cur[1]; | ||
for (int[] d: dir) | ||
{ | ||
int nx = x + d[0]; | ||
int ny = y + d[1]; | ||
|
||
if (nx>=0 && nx<m && ny>=0 && ny<n) | ||
{ | ||
if (step[nx][ny]==0) | ||
{ | ||
q.offer(new int[]{nx, ny}); | ||
step[nx][ny] = steps; | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
pq.offer(new int[]{0, 0, step[0][0]}); | ||
step[0][0] = -1; | ||
|
||
while (!pq.isEmpty()) | ||
{ | ||
int cur[] = pq.poll(); | ||
int x = cur[0]; | ||
int y = cur[1]; | ||
int dist = cur[2]; | ||
|
||
if (x==m-1 && y==n-1) | ||
return dist-1; | ||
|
||
for (int[] d: dir) | ||
{ | ||
int nx = x + d[0]; | ||
int ny = y + d[1]; | ||
|
||
if (nx>=0 && nx<m && ny>=0 && ny<n) | ||
{ | ||
if (step[nx][ny] != -1) | ||
{ | ||
int nsf = Math.min(dist, step[nx][ny]); | ||
pq.offer(new int[]{nx, ny, nsf}); | ||
step[nx][ny] = -1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} |