1123. Anti-Diagonal Totals
A warehouse stores crates on an m x n floor and the supervisor tallies stock along the slanted lines running from the upper right to the lower left. The floor is the integer grid grid. Cells with the same value of row + col lie on the same anti-diagonal.
For every d from 0 to m+n-2, in increasing order of d, compute the sum of all cells with row + col equal to d, and return the m+n-1 sums as an array. A 1 x 1 grid therefore yields a single number.
Constraints: 1 <= m, n <= 50 and -1000 <= grid[i][j] <= 1000. Visiting every cell once and adding it to bucket row + col gives O(m*n) time and O(m+n) extra space.
Example 1
- Input:
- grid = [[1,2,3],[4,5,6],[7,8,9]]
- Output:
- [1,6,15,14,9]
- Explanation:
Diagonals d = 0..4 contain {1}, {2,4}, {3,5,7}, {6,8} and {9}, giving 1, 6, 15, 14, 9.
Example 2
- Input:
- grid = [[1,2,3,4],[5,6,7,8]]
- Output:
- [1,7,9,11,8]
- Explanation:
A 2 x 4 grid has five anti-diagonals: 1, 2+5, 3+6, 4+7 and 8.
Constraints
1 ≤ m, n ≤ 50
-1000 ≤ grid[i][j] ≤ 1000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(m*n)
- Space
- O(m+n)