1944.队列中可以看到的人数
链接:1944.队列中可以看到的人数
难度:Hard
标签:栈、数组、单调栈
简介:请你返回一个长度为 n 的数组 answer ,其中 answer[i] 是第 i 个人在他右侧队列中能 看到 的 人数 。
题解 1 - python
- 编辑时间:2024-01-05
- 执行用时:140ms
- 内存消耗:30.22MB
- 编程语言:python
- 解法介绍:单调栈。
class Solution:
def canSeePersonsCount(self, heights: List[int]) -> List[int]:
n = len(heights)
ans = [0] * n
s = []
for i in range(n):
while s and heights[s[-1]] < heights[i]: ans[s.pop()] += 1
if s: ans[s[-1]] += 1
s.append(i)
return ans