|
| 1 | +''' |
| 2 | +You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. |
| 3 | +
|
| 4 | +The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number. |
| 5 | +
|
| 6 | +Example 1: |
| 7 | +Input: nums1 = [4,1,2], nums2 = [1,3,4,2]. |
| 8 | +Output: [-1,3,-1] |
| 9 | +Explanation: |
| 10 | + For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1. |
| 11 | + For number 1 in the first array, the next greater number for it in the second array is 3. |
| 12 | + For number 2 in the first array, there is no next greater number for it in the second array, so output -1. |
| 13 | +
|
| 14 | +Example 2: |
| 15 | +Input: nums1 = [2,4], nums2 = [1,2,3,4]. |
| 16 | +Output: [3,-1] |
| 17 | +Explanation: |
| 18 | + For number 2 in the first array, the next greater number for it in the second array is 3. |
| 19 | + For number 4 in the first array, there is no next greater number for it in the second array, so output -1. |
| 20 | +
|
| 21 | +Note: |
| 22 | +All elements in nums1 and nums2 are unique. |
| 23 | +The length of both nums1 and nums2 would not exceed 1000. |
| 24 | +
|
| 25 | +''' |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +class Solution: |
| 32 | + def nextGreaterElement(self, nums1, nums2): |
| 33 | + """ |
| 34 | + :type nums1: List[int] |
| 35 | + :type nums2: List[int] |
| 36 | + :rtype: List[int] |
| 37 | + """ |
| 38 | + # method once 双重循环效率较低 |
| 39 | + # Time: O(nlogn) |
| 40 | + # Space: O(n) |
| 41 | + # |
| 42 | + # res = [] |
| 43 | + # length = len(nums2) |
| 44 | + # for i in nums1: |
| 45 | + # if (nums2.index(i)+1) < length: |
| 46 | + # for j in range(nums2.index(i)+1,length): |
| 47 | + # if nums2[j] > i: |
| 48 | + # res.append(nums2[j]) |
| 49 | + # break |
| 50 | + # elif j == length-1: |
| 51 | + # res.append(-1) |
| 52 | + # else: |
| 53 | + # res.append(-1) |
| 54 | + # return res |
| 55 | + |
| 56 | + # method twice 利用字典提升效率 |
| 57 | + # Time: O(nlogn) |
| 58 | + # Space: O(n) |
| 59 | + # |
| 60 | + # if nums2 != []: |
| 61 | + # dic = {nums2[-1]:-1} |
| 62 | + # for index in range(len(nums2)) : |
| 63 | + # for j in range(index+1,len(nums2)): |
| 64 | + # if nums2[index] < nums2[j]: |
| 65 | + # dic[ nums2[index]] = nums2[j] |
| 66 | + # break |
| 67 | + # elif j == len(nums2)-1: |
| 68 | + # dic[nums2[index]] = -1 |
| 69 | + # return [dic.get(i,-1) for i in nums1] |
| 70 | + # return [] |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + # method third 利用栈结构提升效率 |
| 75 | + # Time: O(n) |
| 76 | + # Space: O(n) |
| 77 | + # |
| 78 | + dic , stack = {} ,[] |
| 79 | + for number in nums2: |
| 80 | + while stack and stack[-1] < number: |
| 81 | + dic[stack.pop()] = number |
| 82 | + stack.append(number) |
| 83 | + return [dic.get(i , -1) for i in nums1] |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
0 commit comments