Watch the video solution for Number of People That Can Be Seen in a Grid, a medium level problem involving Array, Stack, Matrix. This walkthrough by Code-Yao has 391 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
(row2, col2) is to the right or below the person at (row1, col1). More formally, this means that either row1 == row2 and col1 < col2 or row1 < row2 and col1 == col2.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.
Example 1:
Input: heights = [[3,1,4,2,5]] Output: [[2,1,2,1,0]] Explanation: - The person at (0, 0) can see the people at (0, 1) and (0, 2). Note that he cannot see the person at (0, 4) because the person at (0, 2) is taller than him. - The person at (0, 1) can see the person at (0, 2). - The person at (0, 2) can see the people at (0, 3) and (0, 4). - The person at (0, 3) can see the person at (0, 4). - The person at (0, 4) cannot see anybody.
Example 2:
Input: heights = [[5,1],[3,1],[4,1]] Output: [[3,1],[2,1],[1,0]] Explanation: - The person at (0, 0) can see the people at (0, 1), (1, 0) and (2, 0). - The person at (0, 1) can see the person at (1, 1). - The person at (1, 0) can see the people at (1, 1) and (2, 0). - The person at (1, 1) can see the person at (2, 1). - The person at (2, 0) can see the person at (2, 1). - The person at (2, 1) cannot see anybody.
Constraints:
1 <= heights.length <= 4001 <= heights[i].length <= 4001 <= heights[i][j] <= 105Problem Overview: You are given an m x n grid where each cell represents a person's height. For every position, count how many people they can see looking to the right in the same row and downward in the same column. A person can see shorter people until someone taller or equal appears, and that blocking person is also visible.
Approach 1: Brute Force Visibility Scan (O(m*n*(m+n)) time, O(1) space)
For each cell, scan to the right and downward while tracking the tallest person seen so far. Continue until you encounter a height greater than or equal to the current person's height. Each shorter person that is taller than all previously seen people is visible, and the first blocking person is also counted. This approach is straightforward but inefficient because every cell performs a linear scan across its row and column.
Approach 2: Monotonic Stack (O(m*n) time, O(m*n) space)
The optimal solution processes each row and column using a monotonic stack. Traverse rows from right to left. Maintain a decreasing stack of heights representing candidates that remain visible. While the stack top is shorter than the current height, pop it and increment the visible count. If the stack still contains an element after popping, that taller or equal person is also visible. Push the current height afterward. Repeat the same idea for each column from bottom to top.
This works because the stack maintains a strictly decreasing sequence of heights. Any shorter person hidden behind a taller one is removed immediately, ensuring each element is pushed and popped at most once. The technique is similar to classic visibility problems solved with stacks in arrays and skyline-style problems.
Handling rows and columns independently converts the problem into two linear passes over the matrix. Each pass uses the same stack logic, producing the right-visibility and downward-visibility counts efficiently.
Recommended for interviews: The monotonic stack solution is what interviewers expect. The brute force approach shows you understand the visibility rule, but its O(m*n*(m+n)) runtime does not scale. The stack-based approach reduces the problem to linear passes with O(m*n) time by ensuring each person enters and leaves the stack once.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Visibility Scan | O(m*n*(m+n)) | O(1) | Good for understanding the visibility rules or very small grids |
| Monotonic Stack (Rows + Columns) | O(m*n) | O(m*n) | Optimal solution for large grids and expected in coding interviews |