1944. Number of Visible People in a Queue

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1944

class Solution:
    def canSeePersonsCount(self, heights: List[int]) -> List[int]:
        n = len(heights)
        res = [0] * n
        stk = []
        for i in range(n - 1, -1, -1):
            while stk and stk[-1] < heights[i]:
                res[i] += 1
                stk.pop()
            if stk:
                res[i] += 1
            stk.append(heights[i])
        return res