-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path496.py
37 lines (30 loc) · 923 Bytes
/
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
#==================================================
#==> Title: next-greater-element-i
#==> Author: Zhang zhen
#==> Email: hustmatnoble.gmail.com
#==> GitHub: https://github.com/MatNoble
#==> Date: 1/15/2021
#==================================================
"""
https://leetcode-cn.com/problems/next-greater-element-i/
"""
class Solution:
def nextGreaterElement(self, nums1, nums2):
n = len(nums1)
res = [-1] * n
nums = {}
for idx, val in enumerate(nums2): nums[val] = idx
for i in range(n):
j = nums[nums1[i]] + 1
while j < len(nums2):
if nums1[i] < nums2[j]:
res[i] = nums2[j]
break
j += 1
return res
nums1 = [4,1,2]
nums2 = [1,3,4,2]
nums1 = [2,4]
nums2 = [1,2,3,4]
mat = Solution()
mat.nextGreaterElement(nums1, nums2)