Repository navigation
Expand file tree
/
Copy pathday62.cpp
More file actions
71 lines (67 loc) · 1.69 KB
/
Copy pathday62.cpp
File metadata and controls
71 lines (67 loc) · 1.69 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// day 67 orginally
#include <vector>
#include <queue>
#include <functional>
#include <stack>
#include <unordered_map>
using namespace std;
//295
class MedianFinder {
public:
priority_queue<int>maxheap;
priority_queue<int,vector<int>,greater<int>>minheap;
MedianFinder() {
}
void addNum(int num) {
if (maxheap.empty() || num<maxheap.top()){
maxheap.push(num);
} else {
minheap.push(num);
}
if (maxheap.size()>minheap.size()+1){
minheap.push(maxheap.top());
maxheap.pop();
}
else if (minheap.size()>maxheap.size()){
maxheap.push(minheap.top());
minheap.pop();
}
}
double findMedian() {
if (maxheap.size()==minheap.size()){
double mean=(maxheap.top()+minheap.top())/2;
return mean;
}
return maxheap.top();
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
// Day 68
class Solution496 {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
stack<int>st;
unordered_map<int,int>mp;
for(int i=nums2.size()-1;i>=0;--i){
while(!st.empty() && st.top()<=nums2[i]){
st.pop();
}
if(st.empty()){
mp[nums2[i]] = -1;
}else{
mp[nums2[i]]=st.top();
}
st.push(nums2[i]);
}
vector<int>ans;
for(int nums:nums1){
ans.push_back(mp[nums]);
}
return ans;
}
};