-
[LeetCode]496. Next Greater Element IAlgorithm/LeetCode 2021. 9. 26. 20:56
The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.
You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.
For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.
Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2] Output: [-1,3,-1] Explanation: The next greater element for each value of nums1 is as follows: - 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. - 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3. - 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4] Output: [3,-1] Explanation: The next greater element for each value of nums1 is as follows: - 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3. - 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.
Constraints:
- 1 <= nums1.length <= nums2.length <= 1000
- 0 <= nums1[i], nums2[i] <= 104
- All integers in nums1 and nums2 are unique.
- All the integers of nums1 also appear in nums2
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: stack = [] for i in nums1: flag = False i_index = nums2.index(i) windowed = nums2[i_index+1:] for k in windowed: if k > i: stack.append(k) flag = True break if flag == False: stack.append(-1) return stack
#memo
전형적인 stack 문제... discussion에 올라와있는 아래 풀이를 보면, 탐색하는 과정에서의 reverse 아이디어,
dict 구조를 활용해서 속도를 올렸다.
#Credit to @navinmittal29 for the following code: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: if not nums2: return None mapping = {} result = [] stack = [] stack.append(nums2[0]) for i in range(1, len(nums2)): while stack and nums2[i] > stack[-1]: # if stack is not empty, then compare it's last element with nums2[i] mapping[stack[-1]] = nums2[i] # if the new element is greater than stack's top element, then add this to dictionary stack.pop() # since we found a pair for the top element, remove it. stack.append(nums2[i]) # add the element nums2[i] to the stack because we need to find a number greater than this for element in stack: # if there are elements in the stack for which we didn't find a greater number, map them to -1 mapping[element] = -1 for i in range(len(nums1)): result.append(mapping[nums1[i]]) return result
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] 169. Majority Element (0) 2021.09.29 [LeetCode]567. Permutation in String (0) 2021.09.26 [LeetCode] 1935. Maximum Number of Words You Can Type (0) 2021.09.25 [LeetCode] 442. Find All Duplicates in an Array (0) 2021.09.25 [LeetCode] 1460. Make Two Arrays Equal by Reversing Sub-arrays (0) 2021.09.25