-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.py
30 lines (27 loc) · 832 Bytes
/
solution.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
"""
15 / 15 test cases passed.
Runtime: 36 ms
Memory Usage: 15.3 MB
"""
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stk, dct = [], {}
for num in nums2:
while stk and stk[-1] < num:
dct[stk.pop()] = num
stk.append(num)
return [dct[num] if num in dct else -1 for num in nums1]
"""
15 / 15 test cases passed.
Runtime: 36 ms
Memory Usage: 15.2 MB
"""
class Solution2:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stk, dct = [], {}
for num in reversed(nums2):
while stk and num >= stk[-1]:
stk.pop()
dct[num] = stk[-1] if stk else -1
stk.append(num)
return [dct[num] for num in nums1]