Given a 2D integer array nums, return all elements of nums in diagonal order as shown in the below images.
solutions
group values by diagonal
def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]:
groups = defaultdict(list)
for i in reversed(range(len(nums))):
for j in range(len(nums[i])):
groups[i+j].append(nums[i][j])
out = []
i = 0
while i in groups:
out.extend(groups[i])
i += 1
return outelegant bfs


def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]:
queue = deque([(0, 0)])
ans = []
while queue:
row, col = queue.popleft()
ans.append(nums[row][col])
if col == 0 and row + 1 < len(nums):
queue.append((row + 1, col))
if col + 1 < len(nums[row]):
queue.append((row, col + 1))
return ans