↓Skip to main content
  1. LeetCode/

LeetCode 1944: Number of Visible People in a Queue

·2 mins· ·
LeetCode Hard Stack Monotonic-Stack
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-09-04

  • Submissions: Accepted → Accepted (retyped the next day)

Approach
#

Person i sees person j to the right when everyone between them is shorter than both.

Process right to left, keeping the stack increasing from top to bottom (taller further down). For person i:

  1. Everyone on top who is shorter is visible to them, and is now hidden behind them for anyone further left, so pop them and count each.
  2. If the stack is still non-empty, that taller person is visible too, but nobody behind them is, so count one more.
  3. Push person i.

My Solution
#

class Solution:
    def canSeePersonsCount(self, heights: List[int]) -> List[int]:
        n = len(heights)
        res = [0] * n
        stack = []
        for i in range(n - 1, -1, -1):
            count = 0
            while stack and stack[-1] < heights[i]:
                stack.pop()
                count += 1
            if stack:
                count += 1
            res[i] = count
            stack.append(heights[i])
        return res

Heights are guaranteed distinct, so equal heights need no handling.

Complexity
#

Time O(n): each person is pushed and popped at most once. Space O(n).

Mistakes to Avoid
#

  • Forgetting to count the taller person who blocks the view after popping.
  • Going left to right: the right side is unknown at that point, so go right to left.
  • Scanning right from every person: O(n^2).

Interview Explanation
#

I keep a monotonic stack from right to left. For each person, everyone on top who is shorter is visible and will be hidden behind them from now on, so I pop and count them; if someone remains, that taller person is visible too, so I add 1. Then I push the current person. Each person enters and leaves once, so time is O(n).

Related

LeetCode 2355: Maximum Number of Books You Can Take
·2 mins
LeetCode Hard Dynamic-Programming Monotonic-Stack Stack Array
LeetCode 2355 note: ending at i, each shelf to the left takes one fewer book until a bottleneck shelf j with too few books; a monotonic stack finds j, and dp[i] = dp[j] + an arithmetic series
LeetCode 3816: Lexicographically Smallest String After Deleting Duplicate Characters
·3 mins
LeetCode Hard Stack Monotonic-Stack Greedy String
LeetCode 3816 note: a monotonic stack that deletes a larger top while another copy exists, then trims duplicate letters off the end since a prefix is always smaller
LeetCode 84: Largest Rectangle in Histogram
·3 mins
LeetCode Daily Hard Array Stack Monotonic-Stack Dynamic-Programming Geometry Histogram
Solving the Largest Rectangle in Histogram problem using monotonic stack approach
LeetCode 1526: Minimum Number of Increments on Subarrays to Form a Target Array
·1 min
LeetCode Hard Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 1526 note: scanning left to right, every rise above the previous height needs that many new operations; drops reuse earlier operations
LeetCode 316: Remove Duplicate Letters
·2 mins
LeetCode Medium Stack Monotonic-Stack Greedy String
LeetCode 316 note: a monotonic stack with last positions; pop a larger top that appears again later, and skip letters already on the stack
LeetCode 726: Number of Atoms
·3 mins
LeetCode Hard Stack String Hash-Map Sorting
LeetCode 726 note: tokenize into elements, numbers, and parentheses; handle parentheses with a stack, with a default multiplier of 1