【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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值