Problem Solving/LeetCode
Today's Challenge
fabichoi
2022. 8. 28. 23:45
https://leetcode.com/problems/sort-the-matrix-diagonally/
Sort the Matrix Diagonally - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
배열(list)의 위치를 잘 활용해서 풀면 되는 문제
class Solution:
def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]:
m, n = len(mat), len(mat[0])
d = defaultdict(list)
for i in range(m):
for j in range(n):
d[i-j].append(mat[i][j])
for k in d:
d[k].sort(reverse=True)
for i in range(m):
for j in range(n):
mat[i][j] = d[i-j].pop()
return mat
반응형