-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
42 lines (26 loc) · 927 Bytes
/
Solution.java
File metadata and controls
42 lines (26 loc) · 927 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
34
35
36
37
38
39
40
41
42
package leetcode.sortMatrixByDiagonals;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
class Solution {
public int[][] sortMatrix(int[][] grid) {
Map<Integer, PriorityQueue<Integer>> diagonals = new HashMap<>();
int colls = grid[0].length, rows = grid.length;
for(int i = 0; i < rows; i++){
for(int j = 0; j < colls; j++){
int key = i - j;
diagonals.putIfAbsent(key, key < 0 ? new PriorityQueue<>()
: new PriorityQueue<>(Collections.reverseOrder()) );
diagonals.get(key).add(grid[i][j]);
}
}
for(int i = 0; i < rows; i++){
for(int j = 0; j < colls; j++){
int key = i - j;
grid[i][j] = diagonals.get(key).poll();
}
}
return grid;
}
}