- Today
- Total
- ์ธํด
- spring
- PS
- Algorithm
- dp
- ๊ตฌํ
- ์์์ ๋ ฌ
- database
- pytorch
- ์กธ์ ์ํ
- BFS
- ๋ฐฑ์๋
- ํ๋ก๊ทธ๋๋จธ์ค
- OOP
- CS
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Graph
- MST
- java
- ์๋ฐ์์ ์
- ๊ทธ๋ฆฌ๋
- ๋ฌธ๋ฒ
- ๋ค์ต์คํธ๋ผ
- tree
- ์๋ฐ
- ๋ฒจ๋งํฌ๋
- array
- leetcode
- ์๋ฃ๊ตฌ์กฐ
- ๋ฐฑ์ค
Partially Committed
[LEETCODE] 496. Next Greater Element I ๋ณธ๋ฌธ
[LEETCODE] 496. Next Greater Element I
WonderJay 2022. 10. 14. 14:19
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;
}
};
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm] Subsequence ํ๋ณํ๊ธฐ (2) | 2022.10.23 |
---|---|
[Algorithm] ๋ฐฐ์ด ๋ด, ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์ ์ ๊ฒ์ํ๊ธฐ (0) | 2022.10.22 |
[LEETCODE] 1790. Check if One String Swap Can Make Strings Equal (0) | 2022.10.13 |
[LEETCODE] 202. Happy Number (0) | 2022.10.12 |
[LEETCODE] 1502. Can Make Arithmetic Progression From Sequence (0) | 2022.10.12 |