Skip to contentKEEP THE CRAFTTHE SANCTUARY

Diagonal Traverse II

Read a jagged grid diagonal by diagonal, going from the bottom row up inside each diagonal.

The problem

Implement Solution.findDiagonalOrder(nums). nums is a list of rows, and the rows can have different lengths (every row holds at least one number). Return all the numbers in diagonal order. Cells (row, col) with the same row + col lie on the same diagonal. Visit the diagonals in increasing order of row + col, and inside one diagonal go from the bottom row up to the top row. For example, [[1, 2, 3], [4, 5, 6], [7, 8, 9]] gives [1, 4, 2, 7, 5, 3, 8, 6, 9]. The grid holds at most 100,000 numbers in total.

Starter code

class Solution:
    def findDiagonalOrder(self, nums):
        pass

Public examples

  1. nums = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]gives[1, 4, 2, 7, 5, 3, 8, 6, 9]
  2. nums = [[7]]gives[7]
  3. nums = [[1], [2, 3], [4]]gives[1, 2, 4, 3]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.