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 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
🏟️ Stadium Vision CalculatorPeople Grid:3Can see: 21Can see: 14Can see: 21Can see: 0Visibility Rules:βœ… Can see people to the RIGHTβœ… Can see people BELOW❌ Tall people block shorter ones behindMagic Formula:πŸ§™β€β™‚οΈ Monotonic Stack tracks the 'skyline'⚑ Process rightβ†’left, bottomβ†’top🎯 O(mn) time complexityBlocked!🎯 Result: Each person knows exactly how many they can see!
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!
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
42.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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