File tree 1 file changed +18
-13
lines changed
1 file changed +18
-13
lines changed Original file line number Diff line number Diff line change 1
1
class Solution (object ):
2
- def nextGreaterElement (self , findNums , nums ):
2
+ def nextGreaterElement (self , nums1 , nums2 ):
3
3
"""
4
- :type findNums : List[int]
5
- :type nums : List[int]
4
+ :type nums1 : List[int]
5
+ :type nums2 : List[int]
6
6
:rtype: List[int]
7
7
"""
8
- if not findNums or not nums :
9
- return []
10
- res = list ()
11
- for item in findNums :
12
- index = nums .index (item )
13
- for i in range (index , len (nums )):
14
- if nums [i ] > item :
15
- res .append (nums [i ])
16
- break
17
- if i + 1 == len (nums ) and nums [- 1 ] <= item :
8
+ mapping = dict ()
9
+
10
+ stack = []
11
+ for num in nums2 :
12
+ while stack and stack [- 1 ] < num :
13
+ top = stack .pop ()
14
+ mapping [top ] = num
15
+ stack .append (num )
16
+
17
+ res = []
18
+ for num in nums1 :
19
+ if num in mapping :
20
+ res .append (mapping [num ])
21
+ else :
18
22
res .append (- 1 )
23
+
19
24
return res
You can’t perform that action at this time.
0 commit comments