## Similar Problems

Similar Problems not available

# Number Of Visible People In A Queue - Leetcode Solution

LeetCode: Number Of Visible People In A Queue Leetcode Solution

Difficulty: Hard

Problem Statement:

You are given an array heights representing the heights of people standing in a queue. The queue is standing in front of a Ferris wheel, and you are able to see some number of people.

You have to find the number of people you can see from the starting of the queue.

For example, if heights = [5,1,2,3,4], then you can see 4 people, because the heights of the people in front of the queue are [5,4,3,2].

Solution:

The problem can be easily solved by iterating over the array from right to left and keeping track of the maximum height seen so far.

We initialize a variable max_height to 0 and a variable count to 0. We iterate over the array from the end to the beginning, for each element i, we check if it is greater than max_height. If it is, then we increment the count and set max_height to i.

After iterating over the array, count variable will have the number of people visible from the starting of the queue.

Code:

The code for the solution will look like the following:

```
def visible_people(heights):
max_height = 0
count = 0
for i in range(len(heights)-1, -1, -1):
if heights[i] > max_height:
count += 1
max_height = heights[i]
return count
```

Time Complexity:

The time complexity of the above solution is O(n), where n is the length of the array heights. This is because we are iterating over the array only once.

## Number Of Visible People In A Queue Solution Code

`1`