Notice
Recent Posts
Recent Comments
Today
Total
01-10 18:14
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[LEETCODE] 496. Next Greater Element I ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[LEETCODE] 496. Next Greater Element I

WonderJay 2022. 10. 14. 14:19
728x90
๋ฐ˜์‘ํ˜•
SMALL

https://leetcode.com/

TITLE : 496. Next Greater Element I

Description : The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.

You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.

For each 0 <= i < nums1.length, find the index j such that nums1[i] == nums2[j] and determine the next greater element of nums2[j] in nums2. If there is no next greater element, then the answer for this query is -1.

Return an array ans of length nums1.length such that ans[i] is the next greater element as described above.

 

Example 1:

Input: nums1 = [4,1,2], nums2 = [1,3,4,2]
Output: [-1,3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.
- 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3.
- 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1.

Example 2:

Input: nums1 = [2,4], nums2 = [1,2,3,4]
Output: [3,-1]
Explanation: The next greater element for each value of nums1 is as follows:
- 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3.
- 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1.

 

Constraints:

  • 1 <= nums1.length <= nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 104
  • All integers in nums1 and nums2 are unique.
  • All the integers of nums1 also appear in nums2.

 

 

 

1. Brute force solution

๊ทธ๋ƒฅ ๋ฌด์ž‘์ • ์ˆœํ™˜ํ•ด์„œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ ์ œํ•œ์ด ๊นŒ๋‹ค๋กญ์ง€ ์•Š์•„์„œ ํ†ต๊ณผํ•  ๊ฒƒ ๊ฐ™์•„์„œ ๋ฐ”๋กœ ๊ตฌํ˜„ํ•ด์„œ ์ œ์ถœํ•˜์˜€๋‹ค.

[C++]

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        vector<int> ans(nums1.size(), -1);
        for (int i = 0; i < nums1.size(); i++) {
            for (int j = 0; j < nums2.size(); j++) {
                if (nums1[i] == nums2[j]) {
                    for (int k = j; k < nums2.size(); k++) {
                        if (nums2[j] < nums2[k]) {
                            ans[i] = nums2[k];
                            break;
                        }
                    }
                }
            }
        }
        return ans;
    }
};

 

 

Follow up: Could you find an O(nums1.length + nums2.length) solution?

Stack ๊ณผ Hash map ์„ ์ด์šฉํ•œ ํ’€์ด๋กœ O(nums1.length + nums2.length)  ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค. 

nums2 ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ stack ์— ์ด์ „ ์›์†Œ๋ฅผ ๋„ฃ๊ณ , while ๋ฌธ์—์„œ NGE(Next greater Element)๋ฅผ ์ฐพ์œผ๋ฉด hash_map ์—

{ stack.top, NGE } ๋ฅผ insert ํ•˜๊ณ  stack ์„ pop ํ•œ๋‹ค. ์ด๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉด hash_map ์—๋Š” ๊ฐ๊ฐ์˜ ์›์†Œ ๋ณ„ NGE ๊ฐ€ key-value ๋กœ ์ €์žฅ๋˜๊ณ , ์ด๋ฅผ ์ด์šฉํ•˜์—ฌ nums1 ๋ฐฐ์—ด์˜ ์›์†Œ์˜ ํ‚ค๊ฐ’์„ ans ์— ์ €์žฅํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

nums1, nums2 ๋ฐฐ์—ด์„ ๊ฐ๊ฐ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ  O(nums1.length + nums2.length) ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

[C++]

// O(N+M)  ; N = nums1.size(), M = nums2.size()

class Solution {
public:
    vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
        stack<int> st;
        map<int, int> hp;
        vector<int> ans(nums1.size());
        for (int i = 0; i < nums2.size(); i++) {
            while (!st.empty()) {
                if (st.top() < nums2[i]) {
                    hp.insert({ st.top(), nums2[i] });
                    st.pop();
                }
                else
                    break;
            }
            st.push(nums2[i]);
        }
        for (int i = 0; i < nums1.size(); i++) {
            if (hp.count(nums1[i])) {
                ans[i] = hp[nums1[i]];
            }
            else {
                ans[i] = -1;
            }
        }
        return ans;
    }
};
728x90
๋ฐ˜์‘ํ˜•
LIST
Comments