Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2282: Number of People That Can Be Seen in a Grid

In this guide, we solve Leetcode #2282 Number of People That Can Be Seen in a Grid in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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).

Quick Facts

  • Difficulty: Medium
  • Premium: Yes
  • Tags: Stack, Array, Matrix, Monotonic Stack

Intuition

We need the next greater or smaller element efficiently, which is exactly what a monotonic stack offers.

Each element is pushed and popped at most once, yielding a linear-time scan.

Approach

Maintain a stack that is either increasing or decreasing, depending on the query.

When the invariant is broken, pop and resolve answers for those indices.

Steps:

  • Scan elements once.
  • Pop while the monotonic condition is violated.
  • Use stack indices to update answers.

Example

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.

Python Solution

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

Complexity

The time complexity is O(m×n)O(m \times n)O(m×n), and the space complexity is O(max⁡(m,n))O(\max(m, n))O(max(m,n)). The space complexity is O(max⁡(m,n))O(\max(m, n))O(max(m,n)).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy