forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortMatixDaigonally.java
More file actions
33 lines (26 loc) · 840 Bytes
/
SortMatixDaigonally.java
File metadata and controls
33 lines (26 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
// TC : O(m*n * log(D)) , D stand min(m, n)
// SC: O(m*n)
public int[][] diagonalSort(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
// i-j
Map<Integer, PriorityQueue<Integer>> map =new HashMap<>();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int diagonalKey = i-j;
PriorityQueue<Integer> pq = map.getOrDefault(diagonalKey, new PriorityQueue<>());
pq.offer(mat[i][j]);
map.put(diagonalKey, pq);
}
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int diagonalKey = i-j;
PriorityQueue<Integer> pq = map.get(diagonalKey);
mat[i][j] =pq.poll();
}
}
return mat;
}
}