Advertisement

Number of Visible People in a Queue - LeetCode 1944 Solution

Number of Visible People in a Queue - Complete Solution Guide

Number of Visible People in a Queue is LeetCode problem 1944, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem asks us to determine, for each person in a queue, how many people they can see to their right. A person 'i' can see another person 'j' to their right (i < j) if all the people standing between them (from i+1 to j-1) are shorter than both person 'i' and person 'j'. We need to return an array where each element represents the number of visible people for the corresponding person in the input array.

Solution Approach

The solution employs a monotonic stack to keep track of potential visible people to the right of each person. The algorithm iterates through the `heights` array from right to left. For each person, it removes shorter people from the stack until the stack is empty or it finds a person taller than the current person. The number of popped elements represents the number of visible people who are shorter than the current person. If the stack is not empty after popping shorter people, the top of the stack is also visible (because it's taller and blocks the view of anyone behind it).

Step-by-Step Algorithm

  1. Step 1: Initialize an empty stack and an array `answer` of the same size as `heights` to store the results.
  2. Step 2: Iterate through the `heights` array from right to left (from index n-1 to 0).
  3. Step 3: For each person at index `i`, initialize a counter `visible_count` to 0.
  4. Step 4: While the stack is not empty and the current person's height (`heights[i]`) is greater than the height of the person at the top of the stack, pop the top element from the stack and increment `visible_count`.
  5. Step 5: If the stack is not empty after the while loop (meaning there's a taller person blocking the view), increment `visible_count`.
  6. Step 6: Assign `visible_count` to `answer[i]`.
  7. Step 7: Push the current person's height (`heights[i]`) onto the stack.
  8. Step 8: After iterating through all the people, return the `answer` array.

Key Insights

  • Insight 1: The key observation is that we can efficiently determine the number of visible people using a stack to maintain a decreasing sequence of heights from right to left.
  • Insight 2: The monotonic stack data structure is crucial for this problem. It allows us to maintain the tallest people that a person can potentially see.
  • Insight 3: Iterating from right to left allows us to progressively build the visibility information by considering the heights to the right of each person.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Stack, Monotonic Stack.

Companies

Asked at: Citigroup, DoorDash, Expedia, GE Healthcare, Rippling, ServiceNow.