Number of People That Can Be Seen in a Grid - Problem
The Stadium Seating Problem: Imagine you're at a stadium with people of different heights sitting in a grid formation. You want to determine how many people each person can see in front of them.
You are given an
1. Line of sight: The second person is either directly to the right (same row, further column) or directly below (same column, further row)
2. Clear view: Everyone between them must be shorter than both the viewer and the person being viewed
Goal: Return a grid where each cell contains the number of people that person can see.
Think of it like counting how many people you can spot from your seat without anyone tall blocking your view!
You are given an
m Γ n grid where each cell contains the height of a person at that position. A person at position (row1, col1) can see another person at position (row2, col2) if:1. Line of sight: The second person is either directly to the right (same row, further column) or directly below (same column, further row)
2. Clear view: Everyone between them must be shorter than both the viewer and the person being viewed
Goal: Return a grid where each cell contains the number of people that person can see.
Think of it like counting how many people you can spot from your seat without anyone tall blocking your view!
Input & Output
example_1.py β 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 2 people: height 4 to the right and height 1 below. Person at (0,1) with height 1 can see 1 person: height 4 to the right. Bottom row people can't see anyone (no one below or to their right).
example_2.py β Single Row
$
Input:
heights = [[1,2,3]]
βΊ
Output:
[[2,1,0]]
π‘ Note:
Person with height 1 can see both people to the right (heights 2 and 3). Person with height 2 can see one person to the right (height 3). Last person sees no one.
example_3.py β Tall Blocker
$
Input:
heights = [[1,5,2,3]]
βΊ
Output:
[[1,2,1,0]]
π‘ Note:
Person with height 1 can only see the tall person (height 5) - the tall person blocks everyone else behind them. The tall person can see heights 2 and 3. Person with height 2 can see height 3.
Constraints
- m == heights.length
- n == heights[i].length
- 1 β€ m, n β€ 1000
- 1 β€ heights[i][j] β€ 105
- All heights are positive integers
Visualization
Tap to expand
Understanding the Visualization
1
Setup the Grid
Place people with different heights in a grid formation
2
Check Horizontal
For each person, count visible people to their right using monotonic stack
3
Check Vertical
For each person, count visible people below them using monotonic stack
4
Combine Results
Sum the counts from both directions for each person
Key Takeaway
π― Key Insight: The monotonic stack acts like a smart 'skyline tracker' - it automatically handles the blocking logic by maintaining only the people who can actually be seen, making the solution both elegant and efficient!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code