-
Notifications
You must be signed in to change notification settings - Fork 0
/
p1944.py
36 lines (28 loc) · 991 Bytes
/
p1944.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# https://leetcode.cn/problems/number-of-visible-people-in-a-queue/description/
import unittest
from typing import List
# 单调栈,倒序遍历
# 维护最小栈
class Solution:
@staticmethod
def canSeePersonsCount(heights: List[int]) -> List[int]:
n: int = len(heights)
ans: list[int] = [0] * n
stack: list[int] = []
for i in range(n - 1, -1, -1):
# 只要栈不为空,且当前元素>栈顶元素
while stack and stack[-1] < heights[i]:
stack.pop()
ans[i] += 1
if stack:
ans[i] += 1
stack.append(heights[i])
return ans
class Test(unittest.TestCase):
def test(self) -> None:
self.assertEqual(
Solution.canSeePersonsCount([10, 6, 8, 5, 11, 9]), [3, 1, 2, 1, 1, 0]
)
self.assertEqual(Solution.canSeePersonsCount([5, 1, 2, 3, 10]), [4, 1, 1, 1, 0])
if __name__ == "__main__":
unittest.main()