Problem 256786 · easy · Phase 02 Linear Data Structures

Anti-Diagonal Sums

2d-lists · diagonals · indexing

An anti-diagonal of a grid runs from the top-right toward the bottom-left. Given a grid grid with R rows and C columns, return a list of length R + C - 1 whose entry k is the sum of all cells grid[r][c] with r + c == k.

Examples

Input:  grid = [[1, 2, 3],
                [4, 5, 6]]
Output: [1, 6, 8, 6]
Explanation: k=0: 1; k=1: 2+4; k=2: 3+5; k=3: 6.

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

Constraints

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

Goals

  • Recognise that cells on one anti-diagonal share the value r + c
  • Accumulate into a list indexed by that value
Starting Python…