Diagonal Traverse II

Given a 2D integer array nums, return all elements of nums in diagonal order: group the elements by diagonal, where the diagonal of element nums[i][j] is i + j. Visit diagonals in increasing order of i + j, and within each diagonal list the elements from the largest row index to the smallest (bottom-left to top-right).

Example 1
Inputnums = [[1,2,3],[4,5,6],[7,8,9]]
Output[1,4,2,7,5,3,8,6,9]
The elements are read by diagonals from top-left to bottom-right in the specified order.
Example 2
Inputnums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
Output[1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
The jagged rows are traversed diagonally in the specified order to produce the returned sequence.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i].length <= 10^5
  • 1 <= sum(nums[i].length) <= 10^5
  • 1 <= nums[i][j] <= 10^5

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate