498. Diagonal Traverse
https://leetcode.com/problems/diagonal-traverse
Python
Re-assemble
- Time: O(n*m)
- Space: O(n*m)
from collections import defaultdict
class Solution:
def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]:
mapper = defaultdict(list)
for i in range(len(mat)):
for j in range(len(mat[i])):
mapper[i+j].append(mat[i][j])
result = []
for key in sorted(mapper.keys()):
if key % 2 == 0:
result += mapper[key][::-1]
continue
result += mapper[key]
return result