Leetcode Problem 2282. Number of People That Can Be Seen in a Grid

2282. Number of People That Can Be Seen in a Grid

Leetcode Solutions

Monotonic Stack Approach

  1. Initialize a 2D array res with the same dimensions as heights to store the result, with all values set to 0.
  2. For each row, create an empty list r to act as a stack.
  3. Iterate over each row from right to left: a. For each element h in the row, pop elements from r while the top of r is less than h and increment res for the current position. b. If the top of r is equal to h, pop the top of r. c. If r is not empty, increment res for the current position. d. Append h to r.
  4. Repeat steps 2-3 for each column, using a separate stack c.
  5. Return the res array.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...