2 parents d7e5f44 + fecf4d7 commit a50ccf5Copy full SHA for a50ccf5
1 file changed
leetcode3/염혜정/1329. Sort the Matrix Diagonally.java
@@ -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
16
17
18
+ mat[i][j] = map.get(key).poll();
19
20
21
22
+ return mat;
23
24
+}
0 commit comments