From 7931778ba8f3d12d097b0f154fbd5532028defce Mon Sep 17 00:00:00 2001 From: JiayangWu <530081999@qq.com> Date: Mon, 3 Feb 2020 22:13:23 -0500 Subject: [PATCH] 2020-02-03 --- ...4\345\244\247\345\205\203\347\264\240I.py" | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git "a/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" "b/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" index b296758..b488d0e 100644 --- "a/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" +++ "b/0496.\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I/0496-\344\270\213\344\270\200\344\270\252\346\233\264\345\244\247\345\205\203\347\264\240I.py" @@ -1,19 +1,24 @@ class Solution(object): - def nextGreaterElement(self, findNums, nums): + def nextGreaterElement(self, nums1, nums2): """ - :type findNums: List[int] - :type nums: List[int] + :type nums1: List[int] + :type nums2: List[int] :rtype: List[int] """ - if not findNums or not nums: - return [] - res = list() - for item in findNums: - index = nums.index(item) - for i in range(index, len(nums)): - if nums[i] > item: - res.append(nums[i]) - break - if i + 1 == len(nums) and nums[-1] <= item: + mapping = dict() + + stack = [] + for num in nums2: + while stack and stack[-1] < num: + top = stack.pop() + mapping[top] = num + stack.append(num) + + res = [] + for num in nums1: + if num in mapping: + res.append(mapping[num]) + else: res.append(-1) + return res \ No newline at end of file