Skip to content

Commit f685179

Browse files
committed
1329. Sort the Matrix Diagonally
1 parent bfc4b77 commit f685179

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
public int[][] diagonalSort(int[][] mat) {
5+
int m = mat.length;
6+
int n = mat[0].length;
7+
8+
Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
9+
10+
for (int i=0; i<m; i++) {
11+
for (int j=0; j<n; j++) {
12+
int key = i-j;
13+
map.putIfAbsent(key, new PriorityQueue<>());
14+
map.get(key).add(mat[i][j]);
15+
}
16+
}
17+
18+
for (int i=0; i<m; i++) {
19+
for (int j=0; j<n; j++) {
20+
int key = i-j;
21+
mat[i][j] = map.get(key).poll();
22+
}
23+
}
24+
25+
return mat;
26+
}
27+
}

0 commit comments

Comments
 (0)