forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
44 lines (32 loc) · 899 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// Source : https://leetcode.com/problems/next-greater-element-i/
/// Author : liuyubobobo
/// Time : 2021-03-08
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
using namespace std;
/// Mono Stack
/// Time Complexity: O(|nums2| + |nums1|)
/// Space Complexity: O(|nums2|)
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> table;
stack<int> stack;
for(int e: nums2){
while(!stack.empty() && e > stack.top()){
table[stack.top()] = e;
stack.pop();
}
stack.push(e);
}
vector<int> res(nums1.size(), -1);
for(int i = 0; i < nums1.size(); i ++)
if(table.count(nums1[i])) res[i] = table[nums1[i]];
return res;
}
};
int main() {
return 0;
}