본문 바로가기
알고리즘/leetcode

496. Next Greater Element I

by 유이얼 2022. 9. 13.
class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> greater;
        stack<int> left;
        
        for (auto n : nums2) {
            while (!left.empty() && left.top() < n) {
                greater[left.top()] = n;
                left.pop();
            }
            left.push(n);
        }
        
        vector<int> ans(nums1.size());
        
        for (int i = 0; i < nums1.size(); ++i) {
            ans[i] = greater.count(nums1[i]) ? greater[nums1[i]] : -1;
        }
        return ans;
    }
};

'알고리즘 > leetcode' 카테고리의 다른 글

953. Verifying an Alien Dictionary  (0) 2022.09.15
1588. Sum of All Odd Length Subarrays  (0) 2022.09.13
57. Insert Interval  (0) 2022.09.01
16. 3Sum Closest  (0) 2022.08.28
417. Pacific Atlantic Water Flow  (0) 2022.08.26