Problem 244289 · medium · Phase 02 Linear Data Structures

List Every Diagonal

2d-lists · diagonals · grouping

A diagonal of a grid runs from the top-left toward the bottom-right. Given a rectangular grid grid, return a list containing every diagonal as a list of its values read top-left to bottom-right. Order the diagonals by their starting cell: begin with the diagonal starting at the top-right corner, move left along the top row, and then continue down the first column.

Examples

Input:  grid = [[1, 2, 3],
                [4, 5, 6]]
Output: [[3], [2, 6], [1, 5], [4]]

Input:  grid = [[1, 2],
                [3, 4]]
Output: [[2], [1, 4], [3]]

Constraints

  • 0 <= rows, cols <= 100; an empty grid gives []

Goals

  • Group cells by the value c - r
  • Emit groups in a specified order
Starting Python…