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.length1 <= n <= 1051 <= heights[i] <= 105heights are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find out how many people each person in a line can see to their right, the brute force approach simply has every single person look straight ahead and count every person standing in front of them until they hit someone taller who blocks their view.
Here's how the algorithm would work step-by-step:
def can_see_persons_count(heights): result_counts = [0] * len(heights) # Iterate through every person in the queue to calculate their individual view count for current_index in range(len(heights)): visible_count = 0 max_seen_height = 0 for scan_index in range(current_index + 1, len(heights)): # Always count the person immediately next in line since no one blocks them yet if scan_index == current_index + 1: visible_count += 1 max_seen_height = heights[scan_index] elif heights[scan_index] > max_seen_height: # Count taller people who manage to peek over previous obstacles in the line visible_count += 1 max_seen_height = heights[scan_index] # Stop scanning rightward the exact moment someone blocks the view entirely if heights[scan_index] >= heights[current_index]: break result_counts[current_index] = visible_count return result_countsInstead of checking every person one by one from the front, the optimal approach works backwards from the end of the line while keeping track of who is currently visible. This clever shortcut avoids repeatedly looking at the same people and lets us figure out how many people each person can see in a single pass.
Here's how the algorithm would work step-by-step:
def can_see_persons_count(heights_of_people):
total_people_count = len(heights_of_people)
visible_count_array = [0] * total_people_count
monotonic_stack = []
# Traverse backwards to maintain a decreasing stack of visible people ahead.
for person_index in range(total_people_count - 1, -1, -1):
people_seen_count = 0
# Remove shorter people who are blocked by the current taller person.
while monotonic_stack and heights_of_people[person_index] > monotonic_stack[-1]:
monotonic_stack.pop()
people_seen_count += 1
# Add one more if a taller person remains to block the further view.
if monotonic_stack:
people_seen_count += 1
visible_count_array[person_index] = people_seen_count
monotonic_stack.append(heights_of_people[person_index])
return visible_count_array| Case | How to Handle |
|---|---|
| Empty or single-element array | Return the array as is or with a single zero since no one is standing to the right. |
| Array with all elements in strictly decreasing order | The monotonic stack will be empty for each person after checking, resulting in an answer of one for each person except the last. |
| Array with all elements in strictly increasing order | Each person will see everyone to their right until the tallest person, resulting in a count of one for all except the last person who sees zero. |
| Array containing identical duplicate heights | The monotonic stack logic handles duplicates by allowing visibility up to and including the first person who is equal or taller. |
| Extremely large array with maximum constraints | The monotonic stack solution processes each element at most twice, ensuring optimal linear time complexity to prevent timeouts. |
| Heights containing negative numbers or zeros | The relational comparison logic works identically regardless of sign since relative height differences dictate visibility. |