-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path496.py
43 lines (39 loc) · 1.34 KB
/
496.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
37
38
39
40
41
42
43
__________________________________________________________________________________________________
sample 44 ms submission
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
stk=[]
d=dict()
for num in nums2:
while len(stk)>0 and num>stk[-1]:
top = stk[-1]
d[top] = num
stk.pop()
stk.append(num)
ans=[]
for num in nums1:
if num not in d: ans.append(-1)
else: ans.append(d[num])
return ans
__________________________________________________________________________________________________
sample 12976 kb submission
#
# @lc app=leetcode id=496 lang=python3
#
# [496] Next Greater Element I
#
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
result = []
for n in nums1:
find_greater = False
ind = nums2.index(n)
for right_n in nums2[ind+1:]:
if right_n > n:
result.append(right_n)
find_greater = True
break
if not find_greater:
result.append(-1)
return result
__________________________________________________________________________________________________