From 049e6fa5b6e94a48c5458e90d54bdafaf65e1da1 Mon Sep 17 00:00:00 2001 From: rimogsu Date: Sat, 12 Sep 2026 13:34:57 +0900 Subject: [PATCH] 1329 --- .../v3/1329. Sort the Matrix Diagonally.py" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "leetcode3/\353\263\200\354\247\200\355\230\221/v3/1329. Sort the Matrix Diagonally.py" diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v3/1329. Sort the Matrix Diagonally.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/1329. Sort the Matrix Diagonally.py" new file mode 100644 index 00000000..05d3f302 --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/1329. Sort the Matrix Diagonally.py" @@ -0,0 +1,60 @@ +''' +1. 아이디어 : +첫번째 행의 마지막 +첫번째 행의 마지막 - 1 -> 두번째 행의 마지막 +첫번째 행의 마지막 - 2 -> 두번째 행의 마지막 - 1 -> 세번 째 행의 마지막 +첫번째 행의 마지막 - 3 -> 두번째 행의 마지막 - 2 -> 세번째 행의 마지막 -1 + 두번째 행의 마지막 - 3 -> 세번째 행의 마지막 -2 + -> 세번째 행의 마지막 -3 +[0][-1] +[0][-2] [1][-1] +[0][-3] [1][-2] [2][-1] +[0][-4] [1][-3] [2][-2] + [1][-4] [2][-3] + [2][-4] + +2. 시간복잡도 : +o(n ^ 2 log n) + +3. 자료구조/알고리즘 : + +''' + +class Solution: + def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]: + x_len = len(mat[0]) + y_len = len(mat) + + dic = {} + lst = [] + j = -1 + for i in range(y_len): + dic[i] = j + j += 1 + + while True: + tmp = [] + for y in range(y_len): + if -x_len <= dic[y] <= -1: + tmp.append((dic[y],y)) + + for k in dic.keys(): + dic[k] -= 1 + lst.append(tmp) + + if all([not(-x_len <= i <= -1) for i in dic.values()]): + break + + print(lst) + + for tmp in lst: + vals = sorted([mat[y][x] for x,y in tmp]) + + while tmp: + x,y = tmp.pop() + v = vals.pop() + + mat[y][x] = v + + return mat + \ No newline at end of file