m × n table grid. Each cell lies on a diagonal running from the top-left towards the bottom-right.
For a cell (r, c) let:
- before be the number of **distinct** values on its diagonal above and to the left (cells (r-1, c-1), (r-2, c-2), …),
- after be the number of distinct values on its diagonal below and to the right ((r+1, c+1), (r+2, c+2), …).
The cell itself counts in neither. Return a table of the same size where cell (r, c) holds |before − after|.grid=[[4, 4, 2], [7, 4, 9], [3, 7, 4]] → [[1, 1, 0], [1, 0, 1], [0, 1, 1]]
Cell (0, 0): nothing up-left (0), down-right holds 4 and 4, one distinct value, |0 - 1| = 1. Cell (1, 1): up-left {4}, down-right {4}, so 0. Cell (2, 2): up-left holds 4 and 4, one distinct value, down-right is empty, so 1.
grid=[[5, 6, 8, 5]] → [[0, 0, 0, 0]]
With a single row each cell is alone on its diagonal, so every answer is 0.
grid=[[1, 2], [3, 1], [2, 3]] → [[1, 0], [1, 1], [0, 1]]
grid=[[4, 4, 2], [7, 4, 9], [3, 7, 4]]
[[1, 1, 0], [1, 0, 1], [0, 1, 1]]
Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.