-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
201 lines (159 loc) · 4.26 KB
/
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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#include <set>
#include <functional>
#include <algorithm>
#include <queue>
template <typename T>
struct reversion_wrapper { T& iterable; };
template <typename T>
auto begin (reversion_wrapper<T> w) { return std::rbegin(w.iterable); }
template <typename T>
auto end (reversion_wrapper<T> w) { return std::rend(w.iterable); }
template <typename T>
reversion_wrapper<T> iter_reverse (T&& iterable) { return { iterable }; }
template <typename T>
std::ostream& operator << (std::ostream& out, const std::vector<T>& vector)
{
for(auto& v : vector)
{
out << v << " ";
}
return out;
}
class circularQueue
{
std::vector<int> history;
size_t hstart;
size_t hend;
size_t qtd;
public:
circularQueue(size_t k) :
history(k), hstart{0}, hend{0}, qtd{0}
{
}
std::tuple<bool,int> push_back(int v)
{
if(isFull())
{
auto remove = history[hstart];
history[hstart] = v;
(++hstart) %= history.size();
(++hend) %= history.size();
return {true, remove};
}
else
{
history[hend] = v;
(++hend) %= history.size();
++qtd;
return {false, 0};
}
}
bool isFull() const { return qtd == history.size(); }
size_t size() const { return qtd; }
};
class slidingWindowMedian
{
size_t qtd;
std::multiset<int, std::greater<int>> smallers;
std::multiset<int, std::less<int>> biggers;
template <typename TContainer>
bool remove(TContainer& multiset, int value)
{
auto itr = multiset.find(value);
if(itr != multiset.end())
{
multiset.erase(itr);
return true;
}
return false;
}
template <typename TA, typename TB>
void move(TA&a, TB&b)
{
auto it = a.begin();
b.insert(*it);
a.erase(it);
}
public:
using value_type = int;
slidingWindowMedian() : qtd{0}
{
}
void push_back(int v)
{
if(qtd == 0)
{
biggers.insert(v);
++qtd;
return;
}
auto median = get_median();
auto insertBigger = v >= median;
if(insertBigger)
biggers.insert(v);
else
smallers.insert(v);
++qtd;
auto qtdhalfsize = qtd / 2;
while(smallers.size() > qtdhalfsize)
move(smallers, biggers);
while(smallers.size() < qtdhalfsize)
move(biggers, smallers);
}
double get_median() const
{
if(qtd == 0) return 0;
double smallest_biggers = *biggers.begin();
if(qtd == 1) return smallest_biggers;
size_t smallers_size = smallers.size();
//if we have items
//balance dictates that we have
//one at biggers
if(qtd % 2 == 1)
{
return smallest_biggers;
}
else
{
//if(smallers_size == 0)
// return smallest_biggers;
double bigger_smallest = *smallers.begin();
return (smallest_biggers + bigger_smallest)/2.0;
}
}
void dump()
{
std::cout << "slidingWindowMedian ";
for(auto& x : iter_reverse(smallers))
std::cout << x << " ";
std::cout << " | ";
for(auto& x : biggers)
std::cout << x << " ";
std::cout << std::endl;
}
};
vector<string> split_string(string);
struct vi { int v; int i;};
std::ostream& operator << (std::ostream& out, const vi& v)
{
return out << "{" << v.v << "," << v.i << "} " ;
}
class MedianFinder {
public:
slidingWindowMedian w;
/** initialize your data structure here. */
MedianFinder() : w{}
{
}
void addNum(int num) { w.push_back(num); }
double findMedian() { return w.get_median(); }
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/