2024-12-11 17:31:43 +08:00

26 lines
659 B
Python

from typing import List
# use monotonic stack
class Solution:
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
n = len(heights)
result = [0] * n
stack = []
# Process from right to left
for i in range(n - 1, -1, -1):
count = 0
# Pop elements shorter than current height
while stack and heights[stack[-1]] < heights[i]:
count += 1
stack.pop()
# If stack not empty, can see one more person
if stack:
count += 1
result[i] = count
stack.append(i)
return result