Posted on

Description

Submission

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> num2idx;

        for(int i = 0; i < nums2.size(); ++i) {
            num2idx[nums2[i]] = i;
        }
        
        vector<int> rets;
        for(auto num: nums1) {
            int ret = -1;
            for(int i = num2idx[num] + 1; i < nums2.size(); ++i) {
                if(nums2[i] > num) {
                    ret = nums2[i];
                    break;
                }
            }
            rets.push_back(ret);
        }
        return rets;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *