problem
solution
option 1 - brute force
1 | class Solution { |
hash table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int> ret;
int n = nums1.size(), m= nums2.size();
unordered_map<int,int> mp;
for(int i=0;i<m;++i) mp[nums2[i]] = i;
for(int i=0;i<n;++i){
int start = mp[nums1[i]], temp=-1;
for(int j=start+1;j<m;++j){
if(nums2[j]>nums1[i]){
temp = nums2[j];
break;
}
}
ret.push_back(temp);
}
return ret;
}
};option 2 - monotonic stack
維護一個單調遞增stack(從stack.top()看起)
需要一個hash table 存取
nums2
各元素及對應索引
1 | class Solution { |
- other version
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int> monoSta;
unordered_map<int,int> index;
int n = nums2.size();
vector<int> ret(n, -1);
for(int i=0;i<n;++i){
index[nums2[i]] = i;
while(!monoSta.empty() && nums2[monoSta.top()] < nums2[i]){
ret[monoSta.top()] = nums2[i];
monoSta.pop();
}
monoSta.push(i);
}
vector<int>ans;
for(int q:nums1){
ans.push_back(ret[index[q]]);
}
return ans;
}
};anaysis
- option 1
- time complexity
O(n^2)
- space complexity
O(1)
- time complexity
- option 2
- time complexity
O(n)
- space complexity
O(n)
- time complexity