Skip to content

Commit a33ae41

Browse files
committed
Runtime: 209 ms (Top 84.06%) | Memory: 75.6 MB (Top 97.21%)
1 parent 9df9836 commit a33ae41

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
// Runtime: 209 ms (Top 84.06%) | Memory: 75.6 MB (Top 97.21%)
12
class Solution {
23
int[] parent;
34
public int[][] matrixRankTransform(int[][] matrix) {
45
int m = matrix.length;
56
int n = matrix[0].length;
67
int[][] answer = new int[m][n];
7-
8+
89
// GROUP BY MATRIX VAL -> {X,Y}
910
TreeMap<Integer, List<int[]>> map = new TreeMap<>();
1011
for(int i = 0; i < m; i++){
@@ -16,97 +17,93 @@ public int[][] matrixRankTransform(int[][] matrix) {
1617
map.get(val).add(xy);
1718
}
1819
}
19-
20-
20+
2121
// INITIALIZE MIN-RANK ARRAY FOR EVERY COL/ROW
2222
int[] minX = new int[m];
2323
int[] minY = new int[n];
24-
24+
2525
for(Integer key : map.keySet()){
2626
List<int[]> list = map.get(key);
27-
27+
2828
// SPLIT TO GROUPS USING UNION FIND FOR VALs IN SAME COL/ROW
2929
int lSize = list.size();
3030
parent = new int[lSize];
3131
for(int i = 0; i < lSize; i++)
3232
parent[i] = i;
33-
33+
3434
// Group the xy by col and row then union by row & by col
3535
HashMap<Integer, List<Integer>> xMap = new HashMap<>();
3636
HashMap<Integer, List<Integer>> yMap = new HashMap<>();
3737
for(int i = 0; i < lSize; i++){
3838
int[] xy = list.get(i);
3939
int x = xy[0];
4040
int y = xy[1];
41-
41+
4242
if(xMap.get(x) == null)
4343
xMap.put(x, new ArrayList<>());
4444
if(yMap.get(y) == null)
4545
yMap.put(y, new ArrayList<>());
4646
xMap.get(x).add(i);
4747
yMap.get(y).add(i);
4848
}
49-
49+
5050
// union by X
5151
for(Integer xKey : xMap.keySet()){
5252
List<Integer> xList = xMap.get(xKey);
5353
for(int i = 1; i < xList.size(); i++){
5454
union(xList.get(i-1), xList.get(i));
5555
}
5656
}
57-
58-
57+
5958
// union by Y
6059
for(Integer yKey : yMap.keySet()){
6160
List<Integer> yList = yMap.get(yKey);
6261
for(int i = 1; i < yList.size(); i++){
6362
union(yList.get(i-1), yList.get(i));
6463
}
6564
}
66-
65+
6766
HashMap<Integer, List<int[]>> group = new HashMap<>();
6867
for(int i = 0; i < lSize; i++){
6968
int grp = find(i);
7069
if(group.get(grp) == null)
7170
group.put(grp, new ArrayList<>());
7271
group.get(grp).add(list.get(i));
7372
}
74-
75-
73+
7674
// SET ANSWER FOR EACH GROUP
7775
for(Integer grpKey : group.keySet()){
7876
int max = 1;
7977
List<int[]> sublist = group.get(grpKey);
80-
78+
8179
// FIND MAX-RANK FOR THIS GROUP
8280
for(int[] xy : sublist){
8381
int x = xy[0];
8482
int y = xy[1];
85-
83+
8684
max = Math.max(max, Math.max(minX[x], minY[y]));
8785
}
88-
86+
8987
// UPDATE ANSWER = MAX-RANK AND SET NEW MIN-RANK FOR ROW/COL = MAX-RANK+1
9088
for(int[] xy : sublist){
9189
int x = xy[0];
9290
int y = xy[1];
9391
answer[x][y] = max;
9492
minX[x] = max+1;
9593
minY[y] = max+1;
96-
}
94+
}
9795
}
9896
}
9997
return answer;
10098
}
101-
102-
99+
103100
// UNION FIND IMPL
104101
void union(int a, int b){
105102
int pa = find(a);
106103
int pb = find(b);
107104
parent[pb] = pa;
108105
}
109-
106+
110107
int find(int a){
111108
int pa = parent[a];
112109
if(pa != a){
@@ -115,4 +112,4 @@ int find(int a){
115112
} else
116113
return a;
117114
}
118-
}
115+
}

0 commit comments

Comments
 (0)