2018年2月4日 星期日

[LeetCode] 496. Next Greater Element I

轉自LeetCode

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.
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.
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
    For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
    For number 1 in the first array, the next greater number for it in the second array is 3.
    For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
    For number 2 in the first array, the next greater number for it in the second array is 3.
    For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
Note:
  1. All elements in nums1 and nums2 are unique.
  2. The length of both nums1 and nums2 would not exceed 1000.
<Solution>

想法如下
  • 先針對 nums2 做歷遍,用 hashmap 記錄每個 element 的 next greater element 是什麼,然用一個 stack 當輔助
  • 例如 nums2 = [1,3,4,2],因為 stack 為空,直接把 1 放到 stack。接下來是 3,因為 stack 不為空,檢查 stack.top 的值(目前是 1) 是不是小於 3,是的話,就記錄 stack.top 這個值的 next greater element 是 3,然後 pop,重複直到 stack 為空,然後再把 3 push 到 stack。接下來是 4,和上個情況一樣。最後是 2,因為 stack.top 目前是 4,比 2 大,所以單純把 2 放到 stack 就好
  • 最後檢查 map,有值的就填值,沒值的就填 -1
code 如下(參考解法)

C++
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
unordered_map<int, int> hashMap;
stack<int> stack;
//>> record next greater element of each element in nums
for(const auto &n : nums) {
while(!stack.empty() && stack.top() < n) {
hashMap[stack.top()] = n;
stack.pop();
}
stack.push(n);
}
//>> get ans
vector<int> ans;
for(const auto &n : findNums) {
ans.push_back(hashMap.count(n) ? hashMap[n] : -1);
}
return ans;
}
};
Java
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();
Stack<Integer> stack = new Stack<>();
//>> record next greater element of each element in nums
for(final int n : nums2) {
while(!stack.empty() && stack.peek() < n) {
map.put(stack.pop(), n);
}
stack.push(n);
}
//>> get ans
for(int i = 0; i < nums1.length; i++) {
nums1[i] = map.getOrDefault(nums1[i], -1);
}
return nums1;
}
}

kotlin
class Solution {
fun nextGreaterElement(nums1: IntArray, nums2: IntArray): IntArray {
val map = mutableMapOf<Int,Int>()
val stack = mutableListOf<Int>()
for(i in nums2.lastIndex downTo 0) {
while(stack.isNotEmpty() && nums2[i] > stack.last()) {
stack.removeAt(stack.lastIndex)
}
map[nums2[i]] = if(stack.isEmpty()) -1 else stack.last()
stack.add(nums2[i])
}
val ans = IntArray(nums1.size)
for(i in nums1.indices) {
ans[i] = map[nums1[i]]!!
}
return ans
}
}

沒有留言:

張貼留言