bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1944. Number of Visible People in a Queue

1944. Number of Visible People in a Queue

Leetcode Solutions

Monotonic Stack Approach

  1. Initialize an empty stack to keep track of the heights of people that are taller than the current person.
  2. Initialize an array answer with the same length as heights to store the number of people each person can see.
  3. Iterate through the heights array from right to left (from the last person to the first person).
  4. For each person, initialize a counter count to 0.
  5. While the stack is not empty and the top of the stack is less than the current person's height, pop the stack and increment count.
  6. If the stack is not empty after popping, increment count by 1 because the current person can see the next taller person.
  7. Push the current person's height onto the stack.
  8. Set answer[i] to count for the current person.
  9. After the loop, return the answer 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...