update
This commit is contained in:
25
leetcode_py/1944.py
Normal file
25
leetcode_py/1944.py
Normal 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
|
||||
Reference in New Issue
Block a user