This commit is contained in:
2024-12-11 17:31:43 +08:00
parent fdaa94a0fa
commit 5e6299439c
6 changed files with 182 additions and 19 deletions

25
leetcode_py/1944.py Normal file
View File

@@ -0,0 +1,25 @@
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