ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://leetcode.com/problems/number-of-visible-people-in-a-queue/ |
| Last Crawled | 2026-04-17 10:24:58 (1 day ago) |
| First Indexed | 2021-07-25 05:32:35 (4 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Number of Visible People in a Queue - LeetCode |
| Meta Description | Can you solve this real interview question? Number of Visible People in a Queue - 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. 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. Example 2: Input: heights = [5,1,2,3,10] Output: [4,1,1,1,0] Constraints: * n == heights.length * 1 <= n <= 105 * 1 <= heights[i] <= 105 * All the values of heights are unique. |
| Meta Canonical | null |
| Boilerpipe Text | 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
i
th
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
i
th
person can see the
j
th
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
i
th
person can
see
to their right in the queue
.
Example 1:
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.
Example 2:
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
. |
| Markdown | 

Debugging...
Submit
00:00:00
[Register](https://leetcode.com/accounts/signup/?next=%2Fproblems%2Fnumber-of-visible-people-in-a-queue%2Fdescription%2F)or[Log in](https://leetcode.com/accounts/login/?next=%2Fproblems%2Fnumber-of-visible-people-in-a-queue%2Fdescription%2F)
[Premium](https://leetcode.com/subscribe/?ref=lp_pl&source=qd)
Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Code
Code
Testcase
Testcase
Test Result
Test Result
[1944\. Number of Visible People in a Queue](https://leetcode.com/problems/number-of-visible-people-in-a-queue/)
Hard
Topics
Companies
Hint
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*.
**Example 1:**

```
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.
```
**Example 2:**
```
Input: heights = [5,1,2,3,10]
Output: [4,1,1,1,0]
```
**Constraints:**
- `n == heights.length`
- `1 <= n <= 105`
- `1 <= heights[i] <= 105`
- All the values of `heights` are **unique**.
Accepted
126,114/173.1K
Acceptance Rate
72\.8%
***
Topics
Senior Staff
[Array](https://leetcode.com/tag/array/)[Stack](https://leetcode.com/tag/stack/)[Monotonic Stack](https://leetcode.com/tag/monotonic-stack/)[Biweekly Contest 57](https://leetcode.com/contest/biweekly-contest-57/)
***

Companies
***
Hint 1
How to solve this problem in quadratic complexity ?
***
Hint 2
For every subarray start at index i, keep finding new maximum values until a value larger than arr\[i\] is found.
***
Hint 3
Since the limits are high, you need a linear solution.
***
Hint 4
Use a stack to keep the values of the array sorted as you iterate the array from the end to the start.
***
Hint 5
Keep popping from the stack the elements in sorted order until a value larger than arr\[i\] is found, these are the ones that person i can see.
***
Similar Questions
[Buildings With an Ocean View](https://leetcode.com/problems/buildings-with-an-ocean-view/)
Medium
[Sum of Subarray Ranges](https://leetcode.com/problems/sum-of-subarray-ranges/)
Medium
[Sum of Total Strength of Wizards](https://leetcode.com/problems/sum-of-total-strength-of-wizards/)
Hard
[Number of People That Can Be Seen in a Grid](https://leetcode.com/problems/number-of-people-that-can-be-seen-in-a-grid/)
Medium
[Find Building Where Alice and Bob Can Meet](https://leetcode.com/problems/find-building-where-alice-and-bob-can-meet/)
Hard
***
Discussion (89)
Choose a type
Comment
💡 Discussion Rules
1\. Please don't post **any solutions** in this discussion.
2\. The problem discussion is for asking questions about the problem or for sharing tips - anything except for solutions.
3\. If you'd like to share your solution for feedback and ideas, please head to the solutions tab and post it there.
Sort by:Best
No comments yet.
1
2
3
4
9
Copyright © 2026 LeetCode. All rights reserved.
2\.2K
89
26 Online
1
Ln 1, Col 1
Case 1
Case 2
heights =
\[10,6,8,5,11,9\]
9
1
2
›
\[10,6,8,5,11,9\]
\[5,1,2,3,10\]
Source
FindHeaderBarSize
FindTabBarSize
FindBorderBarSize |
| Readable Markdown | 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*.
**Example 1:**

```
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.
```
**Example 2:**
```
Input: heights = [5,1,2,3,10]
Output: [4,1,1,1,0]
```
**Constraints:**
- `n == heights.length`
- `1 <= n <= 105`
- `1 <= heights[i] <= 105`
- All the values of `heights` are **unique**. |
| Shard | 99 (laksa) |
| Root Hash | 17128001481954483899 |
| Unparsed URL | com,leetcode!/problems/number-of-visible-people-in-a-queue/ s443 |