Tap here to switch tabs
Problem
Submissions

Problem: Number of People That Can Be Seen in a Grid

Try to solve the Number of People That Can Be Seen in a Grid problem.

Statement

You are given an m×nm \times n 00 indexed 2D2 {D} array of integers heights representing the heights of people standing in a grid.

A person at cell (r1,c1)(r_1, c_1) can see a person at cell (r2,c2)(r_2, c_2) if they are either in the same row looking to the right or in the same column looking downwards. Specifically, the person at (r1,c1)(r_1, c_1) can see the person at (r2,c2)(r_2, c_2) if:

  • In the same row: r1=r2r_1 = r_2, c1<c2c_1 < c_2, and every person in cell (r1,k)(r_1, k) with c1<k<c2c_1 < k < c_2 is strictly shorter than min⁡(\min(heights[r1][c1],[r_1][c_1], heights[r2][c2])[r_2][c_2]).

  • In the same column: c1=c2c_1 = c_2, r1<r2r_1 < r_2, and every person in cell (k,c1)(k, c_1) with r1<k<r2r_1 < k < r_2 is strictly shorter than min⁡(\min(heights[r1][c1],[r_1][c_1], heights[r2][c2])[r_2][c_2]).

Return an m×nm \times n 2D2 {D} array of integers ans where ans[i][j] is the total number of people that the person at cell (i,j)(i, j) can see to their right and downwards.

Constraints:

  • 1≤1 \leq heights.length ≤400\leq 400

  • 1≤1 \leq heights[i].length ≤400\leq 400

  • 1≤1 \leq heights[i][j] ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Number of People That Can Be Seen in a Grid

Try to solve the Number of People That Can Be Seen in a Grid problem.

Statement

You are given an m×nm \times n 00 indexed 2D2 {D} array of integers heights representing the heights of people standing in a grid.

A person at cell (r1,c1)(r_1, c_1) can see a person at cell (r2,c2)(r_2, c_2) if they are either in the same row looking to the right or in the same column looking downwards. Specifically, the person at (r1,c1)(r_1, c_1) can see the person at (r2,c2)(r_2, c_2) if:

  • In the same row: r1=r2r_1 = r_2, c1<c2c_1 < c_2, and every person in cell (r1,k)(r_1, k) with c1<k<c2c_1 < k < c_2 is strictly shorter than min⁡(\min(heights[r1][c1],[r_1][c_1], heights[r2][c2])[r_2][c_2]).

  • In the same column: c1=c2c_1 = c_2, r1<r2r_1 < r_2, and every person in cell (k,c1)(k, c_1) with r1<k<r2r_1 < k < r_2 is strictly shorter than min⁡(\min(heights[r1][c1],[r_1][c_1], heights[r2][c2])[r_2][c_2]).

Return an m×nm \times n 2D2 {D} array of integers ans where ans[i][j] is the total number of people that the person at cell (i,j)(i, j) can see to their right and downwards.

Constraints:

  • 1≤1 \leq heights.length ≤400\leq 400

  • 1≤1 \leq heights[i].length ≤400\leq 400

  • 1≤1 \leq heights[i][j] ≤105\leq 10^5