【LEETCODE】496. Next Greater Element I

問題描述

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:
All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.

解法1 暴力 O(N^2)

對每個待找元素,都在父集中先找到該元素位置然後從該位置起往後找第一個大於它的元素

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
        vector<int> res;
        int length1 = findNums.size(), length2 = nums.size();
        for (int i = 0; i < length1; i++) {
            int num = findNums[i];
            int Found = -1;
            bool flag = false;
            for (int j = 0; j < length2; j++) {
                if (nums[j] == num)
                    flag = true;
                if (flag && nums[j] > num) {
                    Found = nums[j];
                    break;
                }       
            }
            res.push_back(Found);
        }
        return res;
    }
};

解法2

只對父集進行一次遍歷,爲父集的每個元素都確定NGE。
算法思想爲:維護一個降序的棧temps,先將父集中第一個元素壓棧,並從第二個元素開始往後遍歷父集。
若遍歷到的元素CurrentNum比棧頂的小,則temps仍是降序棧,直接將該元素壓棧;
否則,出棧直到遍歷到的元素比棧頂的元素小爲止,並邊出棧邊用map記錄NGE(將棧頂元素的NGE記錄爲遍歷到的元素,通過建立棧頂元素與遍歷到的元素的映射)。


class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
        vector<int> res; // result array
        map<int,int> mymap; // used to store the next greater elements of all
        stack<int> TempS; // to maintain a descending stack
        if (nums.size() <= 0)
            return res;
        TempS.push(nums[0]);
        for (int i = 1; i < nums.size(); i++) {
            int CurrentNum = nums[i];
            if (CurrentNum <= TempS.top())  {}
                
            else { // when currentnum is greater than stack.top(), 
                //currentnum would be the greater ele for all nums in the stack
                while (!TempS.empty() && CurrentNum >= TempS.top()) {
                    mymap.insert(pair<int,int>(TempS.top(),CurrentNum));
                    TempS.pop();
                }
            }
            TempS.push(CurrentNum);
        }
        map<int,int>::iterator it;
        for (int i = 0 ; i < findNums.size(); i++) {
            it = mymap.find(findNums[i]);
            if (it != mymap.end())
                res.push_back(mymap[findNums[i]]);
            else
                res.push_back(-1);
        }
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章