Number of People That Can Be Seen in a Grid Problem
Description
LeetCode Problem 2282.
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 (row_1, col_1) can see a person standing at position (row_2, col_2) if:
- The person at
(row_2, col_2)is to the right or below the person at(row_1, col_1). More formally, this means that eitherrow_1 == row_2andcol_1 < col_2orrow_1 < row_2andcol_1 == col_2. - 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.
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] <= 10^5
Solution
Approach: Monotonic Stack on Every Row and Column
A person sees someone to the right (or below) when everyone in between is strictly shorter than both of them. Looking along one line from the far end, this is the classic "visible people in a queue" pattern, so each row and each column can be handled on its own with a monotonic stack; the two counts are then added.
For one line, go from the end towards the start and keep a stack of heights that is decreasing from bottom to top:
- While the top is smaller than the current height, the current person sees it. Count it and pop it.
- If the stack is still not empty, its top is at least as tall as the current person and is also seen, so add one more.
- If that top has exactly the same height, pop it. The same height can never be seen past by anyone to the left, and the person that just arrived takes its place.
- Push the current height.
Run this on every row, then on every column, and add the two results per cell.
Complexity: O(m · n) time and O(max(m, n)) extra space for the stack, where the grid is m x n.
class Solution:
def seePeople(self, heights: List[List[int]]) -> List[List[int]]:
def f(nums: List[int]) -> List[int]:
n = len(nums)
stk = []
ans = [0] * n
for i in range(n - 1, -1, -1):
while stk and stk[-1] < nums[i]:
ans[i] += 1
stk.pop()
if stk:
ans[i] += 1
while stk and stk[-1] == nums[i]:
stk.pop()
stk.append(nums[i])
return ans
ans = [f(row) for row in heights]
m, n = len(heights), len(heights[0])
for j in range(n):
add = f([heights[i][j] for i in range(m)])
for i in range(m):
ans[i][j] += add[i]
return ans