Skip to content

Commit a50ccf5

Browse files
Merge pull request #2016 from CodingTestStudy2/염혜정
[염혜정] Day12
2 parents d7e5f44 + fecf4d7 commit a50ccf5

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[][] diagonalSort(int[][] mat) {
3+
int m = mat.length, n = mat[0].length;
4+
Map<Integer, PriorityQueue<Integer>> map = new HashMap<>();
5+
6+
// 1. 대각선별로 값 모으기 (key = row - col)
7+
for (int i = 0; i < m; i++) {
8+
for (int j = 0; j < n; j++) {
9+
int key = i - j;
10+
map.computeIfAbsent(key, k -> new PriorityQueue<>()).offer(mat[i][j]);
11+
}
12+
}
13+
14+
// 2. 정렬된 값 다시 채워넣기
15+
for (int i = 0; i < m; i++) {
16+
for (int j = 0; j < n; j++) {
17+
int key = i - j;
18+
mat[i][j] = map.get(key).poll();
19+
}
20+
}
21+
22+
return mat;
23+
}
24+
}

0 commit comments

Comments
 (0)