496. 下一个更大元素 I
为保证权益,题目请参考 496. 下一个更大元素 I(From LeetCode).
解决方案1
Python
python
# 496. 下一个更大元素 I
# https://leetcode-cn.com/problems/next-greater-element-i/
################################################################################
from typing import List
from functools import reduce
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stacks = []
dDict = {}
for n in reversed(nums2):
if len(stacks) == 0:
stacks.append(n)
dDict[n] = -1
else:
if n < stacks[-1]:
dDict[n] = stacks[-1]
stacks.append(n)
else:
while len(stacks) > 0 and n >= stacks[-1]:
stacks.pop()
if len(stacks) == 0:
dDict[n] = -1
else:
dDict[n] = stacks[-1]
stacks.append(n)
ans = list(map(lambda x: dDict[x], nums1))
return ans
################################################################################
if __name__ == "__main__":
solution = Solution()
print([] == False)
print([] == False)
print([] is False)
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
37
38
39
40
41
42
43
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
37
38
39
40
41
42
43