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
heights
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:
Imagine people standing in a line, and you want to figure out how many people each person can see in front of them. The straightforward but inefficient way is to have each person look at every single person in front of them individually. This is the brute force approach.
Here's how the algorithm would work step-by-step:
def visible_people(heights):
number_of_people = len(heights)
visible_counts = [0] * number_of_people
for current_person_index in range(number_of_people):
for other_person_index in range(current_person_index + 1, number_of_people):
is_visible = True
# Check if the other person is visible
for intermediate_person_index in range(current_person_index + 1, other_person_index):
# If any intermediate person is taller, other person is not visible
if heights[intermediate_person_index] >= heights[other_person_index]:
is_visible = False
break
# Increment the visible count if no one blocks the view
if is_visible:
visible_counts[current_person_index] += 1
return visible_counts
The key idea is to maintain a decreasing stack to keep track of potentially visible people. We iterate through the queue, and for each person, we pop anyone shorter than them from the stack (because they are blocked).
Here's how the algorithm would work step-by-step:
def visible_people(heights):
number_of_people = len(heights)
visible_counts = [0] * number_of_people
stack = []
for i in range(number_of_people):
while stack and heights[stack[-1]] < heights[i]:
# Pop shorter people, since they are blocked.
stack.pop()
visible_counts[i] += 1
if stack:
# Account for visibility of the next tallest person.
visible_counts[i] += 1
# Push the current person onto the stack.
stack.append(i)
return visible_counts
Case | How to Handle |
---|---|
Null or empty input array | Return an empty array since there are no people to consider. |
Input array with only one element | Return an array containing only 0, as a single person can see no one to their right. |
Input array with all elements having the same height | Each person can only see the immediate neighbor to the right unless it's the last person, who sees 0. |
Input array with heights in strictly increasing order | Each person can see all the people to their right. |
Input array with heights in strictly decreasing order | Each person can only see one person to the right, the next one. |
Maximum size of the input array (scalability) | The solution should be optimized to avoid quadratic time complexity to handle large input sizes efficiently, ideally using a stack based approach. |
Heights with extremely large values (integer overflow) | Ensure that the comparisons and calculations do not lead to integer overflow, potentially using a larger integer type if necessary. |
Combination of tall people followed by many shorter people | Stack should efficiently remove shorter people from its base to accurately calculate the number of visible people for the taller ones. |