> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Visible People in a Queue

> Tested Python solution for LeetCode 1944 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1944, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Stack](/catalog/topics/stack), [Monotonic Stack](/catalog/topics/monotonic-stack). [View on LeetCode](https://leetcode.com/problems/number-of-visible-people-in-a-queue/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1944   # by problem number
lcpy gen -s number_of_visible_people_in_a_queue   # by problem name
```

## Problem

There are `n` people standing in a queue, and they numbered from `0` to `n - 1` in **left to right** order. You are given an array `heights` of **distinct** integers where `heights[i]` represents the height of the `ith` person.

A person can **see** another person to their right in the queue if everybody in between is **shorter** than both of them. More formally, the `ith` person can see the `jth` person if `i < j` and `min(heights[i], heights[j]) > max(heights[i+1], heights[i+2], ..., heights[j-1])`.

Return *an array* `answer` *of length* `n` *where* `answer[i]` *is the **number of people** the `ith` person can **see** to their right in the queue*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/05/29/queue-plane.jpg)

```
Input: heights = [10,6,8,5,11,9]
Output: [3,1,2,1,1,0]
```

**Explanation:**

* Person 0 can see person 1, 2, and 4.
* Person 1 can see person 2.
* Person 2 can see person 3 and 4.
* Person 3 can see person 4.
* Person 4 can see person 5.
* Person 5 can see no one since nobody is to the right of them.

```
Input: heights = [5,1,2,3,10]
Output: [4,1,1,1,0]
```

### Constraints

* `n == heights.length`
* `1 <= n <= 10^5`
* `1 <= heights[i] <= 10^5`
* All the values of `heights` are **unique**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_visible_people_in_a_queue/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_visible_people_in_a_queue/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def can_see_persons_count(self, heights: list[int]) -> list[int]:
        n = len(heights)
        answer = [0] * n
        stack: list[int] = []
        for i in range(n - 1, -1, -1):
            height = heights[i]
            while stack and stack[-1] < height:
                stack.pop()
                answer[i] += 1
            if stack:
                answer[i] += 1
            stack.append(height)
        return answer
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.