Skip to content

Commit 7931778

Browse files
committed
2020-02-03
1 parent f57e210 commit 7931778

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
class Solution(object):
2-
def nextGreaterElement(self, findNums, nums):
2+
def nextGreaterElement(self, nums1, nums2):
33
"""
4-
:type findNums: List[int]
5-
:type nums: List[int]
4+
:type nums1: List[int]
5+
:type nums2: List[int]
66
:rtype: List[int]
77
"""
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:
1822
res.append(-1)
23+
1924
return res

0 commit comments

Comments
 (0)