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

You are given an m x n 0-indexed 2D array of positive integers heights where heights[i][j] is the height of the person standing at position (i, j).

A person standing at position (row1, col1) can see a person standing at position (row2, col2) if:

  • The person at (row2, col2) is to the right or below the person at (row1, col1). More formally, this means that either row1 == row2 && col1 < col2 or row1 < row2 && col1 == col2.
  • Everyone in between them is shorter than both of them.

Return an m x n 2D array of integers answer where answer[i][j] is the number of people that the person at position (i, j) can see.

Input & Output

Example 1 — Basic Grid
$ Input: heights = [[3,1,4,2,5],[1,2,3,4,5]]
Output: [[2,1,2,1,0],[0,0,0,0,0]]
💡 Note: Person at (0,0) with height 3 can see (0,1) height 1 and (1,0) height 1. Person at (0,1) with height 1 can see (0,2) height 4. Person at (0,2) with height 4 can see (0,3) height 2 and (1,2) height 3. Person at (0,3) with height 2 can see (0,4) height 5. Last column and bottom row see 0 people.
Example 2 — Single Row
$ Input: heights = [[1,2,3]]
Output: [[2,1,0]]
💡 Note: Person at (0,0) height 1 can see both (0,1) height 2 and (0,2) height 3. Person at (0,1) height 2 can see (0,2) height 3. Person at (0,2) sees no one.
Example 3 — Blocking Heights
$ Input: heights = [[5,1,2,3,4],[1,1,1,1,1]]
Output: [[4,3,2,1,0],[0,0,0,0,0]]
💡 Note: Person at (0,0) height 5 can see everyone to the right since they're all shorter. Bottom row can't see anyone since they're at the last row and no one is to their right in same row.

Constraints

  • 1 ≤ m, n ≤ 50
  • 1 ≤ heights[i][j] ≤ 105

Visualization

Tap to expand
Number of People That Can Be Seen in a Grid INPUT heights[][] grid (m x n) 3 1 4 2 5 1 2 3 4 5 Visibility Rules: 1. Can see RIGHT or BELOW 2. All between must be shorter than BOTH 3 1 4 3 sees 1, then 4 (1 is shorter than both) ALGORITHM STEPS 1 Use Monotonic Stack Process each row/col separately 2 Process Right to Left For rows: iterate backwards 3 Pop Shorter Elements Count visible people while popping 4 Check Taller Element If stack not empty, add 1 more Stack Example (Row 0): i=4: stack=[5] answer[0][4]=0 i=3: stack=[5,2] ans[0][3]=1 i=2: stack=[5,4] ans[0][2]=2 i=1: stack=[5,4,1] ans[0][1]=1 i=0: stack=[5,4,3] ans[0][0]=2 FINAL RESULT answer[][] - visible people count 2 1 2 1 0 0 0 0 0 0 Row 0 Explanation: [0,0]=2: Person 3 sees 1 and 4 [0,1]=1: Person 1 sees only 4 [0,2]=2: Person 4 sees 2 and 5 [0,3]=1: Person 2 sees only 5 [0,4]=0: Person 5 sees no one Row 1: All zeros (increasing) Each person blocked by next taller person immediately right Output: [[2,1,2,1,0],[0,0,0,0,0]] Key Insight: The monotonic decreasing stack efficiently tracks visible people. When processing right-to-left, we pop all shorter elements (each counts as visible), then check if a taller one remains (also visible). This gives O(m*n) time complexity since each element is pushed and popped at most once per row and column processing. TutorialsPoint - Number of People That Can Be Seen in a Grid | Optimal Solution (Monotonic Stack)
Asked in
Amazon 15 Google 12 Microsoft 8
12.5K Views
Medium Frequency
~25 min Avg. Time
467 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen