m × n formation; heights[r][c] is the height of the person in row r, column c. Everyone looks only to the right along their row and only down along their column.
The person at (r, c) can see the person at (r2, c2) when:
- either r2 = r and c2 > c, or c2 = c and r2 > r;
- everyone standing strictly between them is shorter than both of them.
Return an m × n matrix whose cell (r, c) holds how many people the person at (r, c) can see.heights=[[3, 1, 2], [2, 4, 1]] → [[3, 2, 1], [1, 1, 0]]
Person (0, 0), height 3, sees (0, 1) and (0, 2) to the right: the only one in between is 1 tall, shorter than both. Below, they see (1, 0). That makes 3. Person (1, 1), height 4, sees only (1, 2).
heights=[[5, 5, 5]] → [[1, 1, 0]]
Everyone sees the neighbour to the right but no further: a neighbour of the same height blocks the view.
heights=[[3, 1, 2], [2, 4, 1]]
[[3, 2, 1], [1, 1, 0]]
Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.