-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign.cpp
More file actions
403 lines (331 loc) · 9.3 KB
/
design.cpp
File metadata and controls
403 lines (331 loc) · 9.3 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include<bits/stdc++.h>
using namespace std;
class LRUCache {
public:
const int capacity;
list<pair<int,int>> l;
unordered_map<int, list<pair<int,int > >::iterator> m;
int remove(list<pair<int,int > >::iterator it) {
int key = it->first;
int val = it->second;
l.erase(it);
m.erase(key);
return val;
}
void add(const int & key, const int & value) {
l.push_back({key, value});
m[key] = --(l.end());
}
LRUCache(int capacity) : capacity(capacity) {}
int get(int key) {
if(!m.count(key)) return -1;
int value = remove(m[key]);
add(key, value);
return value;
}
void put(int key, int value) {
if(m.count(key)) remove(m[key]);
if(l.size() == capacity) remove(l.begin());
add(key, value);
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
class MinStack {
public:
/** initialize your data structure here. */
stack<int> s;
stack<int> mn;
MinStack() {
}
void push(int x) {
if(mn.empty() || mn.top() >= x) mn.push(x);
s.push(x);
}
void pop() {
if(mn.size() && mn.top() == s.top()) mn.pop();
s.pop();
}
int top() {
return s.top();
}
int getMin() {
return mn.top();
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
class MedianFinder {
public:
/** initialize your data structure here. */
priority_queue<int> fHalf;
priority_queue<int, vector<int>, greater<int> > sHalf;
MedianFinder() {
}
void addNum(int num) {
if(fHalf.size() && fHalf.top() >= num) fHalf.push(num);
else sHalf.push(num);
int n = fHalf.size(), m = sHalf.size();
if(m && n < m) fHalf.push(sHalf.top()), sHalf.pop();
else if(n && m < n - 1) sHalf.push(fHalf.top()), fHalf.pop();
}
double findMedian() {
int n = fHalf.size() + sHalf.size();
double ret = fHalf.top();
if(n & 1) {
return ret;
} else {
return (ret + sHalf.top()) / 2;
}
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Codec {
public:
string vToS(TreeNode * cur) {
if(!cur) return "-";
return to_string(cur->val);
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
queue<TreeNode * > q;
q.push(root);
string s = "";
while(!q.empty()) {
int sz = q.size();
string t = "";
bool nullLvl = true;
for(int i = 0; i < sz; ++i) {
if(t.size()) t += ",";
TreeNode * cur = q.front(); t += vToS(cur);
q.pop();
if(cur) {
q.push(cur->left), q.push(cur->right);
nullLvl &= (!cur->left && !cur->right);
}
}
s = s + (s.empty() ? "" : ",") + t;
}
cout << s << endl;
return s;
}
TreeNode * sToN(string s) {
return s == "-" ? NULL : new TreeNode(atoi(s.c_str()));
}
TreeNode * construct(const vector<string> & v) {
vector<TreeNode *> pre;
TreeNode * root = sToN(v[0]);
pre.push_back(root);
int idx = 1;
while(idx < v.size()) {
vector<TreeNode *> cur;
for(auto n: pre) {
n->left = sToN(v[idx++]);
n->right = sToN(v[idx++]);
if(n->left) cur.push_back(n->left);
if(n->right) cur.push_back(n->right);
}
swap(pre, cur);
}
return root;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
regex re(",");
vector<string> v {
sregex_token_iterator(data.begin(), data.end(), re, -1),
sregex_token_iterator()
};
return construct(v);
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
class TicTacToe {
public:
static const int P_N = 2;
static const int B_SIZE = 3;
vector<int> rows[P_N];
vector<int> cols[P_N];
int dig[P_N];
int adig[P_N];
int n;
/** Initialize your data structure here. */
TicTacToe(int n) {
for(int i = 0; i < P_N; ++i)
rows[i] = vector<int>(n, 0),
cols[i] = vector<int>(n, 0),
dig[i] = adig[i] = 0;
this->n = n;
}
/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
int move(int row, int col, int player) {
--player;
rows[player][row]++;
cols[player][col]++;
dig[player] += (row == col);
adig[player] += (row + col == n - 1);
if(rows[player][row] == n || cols[player][col] == n || dig[player] == n || adig[player] == n) return player + 1;
return 0;
}
};
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe* obj = new TicTacToe(n);
* int param_1 = obj->move(row,col,player);
*/
constexpr int SIZE = (1 << 7);
struct Trie {
int freq;
Trie * next[SIZE];
map<string, int> res;
Trie() {
freq = 0;
memset(next, 0, sizeof next);
}
int insert(const string & s, const int & idx, const int & df) {
if(idx == s.length()) {
res[s] += df;
return res[s];
}
if(!next[s[idx]]) next[s[idx]] = new Trie();
int freq = next[s[idx]]->insert(s, idx + 1, df);
res[s] = freq;
return freq;
}
vector<string> explore(const string & s, const int & idx = 0) {
if(idx == s.length()) {
vector<string> v;
auto cmp = [](const pair<int, string> & a, const pair<int, string> & b) -> bool {
if(a.first == b.first) return a.second > b.second;
return a.first < b.first;
};
priority_queue<pair<int, string>, vector<pair<int, string> >, decltype(cmp) > pq(cmp);
for(auto p: res) {
pq.push({p.second, p.first});
}
int sz = min(3, (int)pq.size());
for(int i = 0; i < sz; ++i) {
v.push_back(pq.top().second);
pq.pop();
}
return v;
}
if(!next[s[idx]]) return {};
return next[s[idx]]->explore(s, idx + 1);
}
};
class AutocompleteSystem {
public:
string cur;
Trie * root;
AutocompleteSystem(vector<string>& s, vector<int>& times) {
root = new Trie();
cur = "";
for(int i = 0; i < s.size(); ++i) {
root->insert(s[i], 0, times[i]);
}
}
vector<string> input(char c) {
if(c == '#') {
root->insert(cur, 0, 1);
cur = "";
return {};
} else {
cur += string(1, c);
return root->explore(cur);
}
}
};
/**
* Your AutocompleteSystem object will be instantiated and called as such:
* AutocompleteSystem* obj = new AutocompleteSystem(sentences, times);
* vector<string> param_1 = obj->input(c);
*/
class FreqStack {
public:
priority_queue<tuple<int,int,int>> s;
map<int,int> mp;
int ts;
FreqStack() {
ts = 0;
}
void push(int x) {
mp[x]++;
s.push({mp[x], ts++, x});
}
int pop() {
int val = get<2>(s.top());
s.pop();
mp[val]--;
return val;
}
};
class FreqStackOpt {
public:
unordered_map<int, int> freq;
unordered_map<int, stack<int> > s;
int mxFreq;
FreqStackOpt() {
mxFreq = 0;
}
void push(int x) {
mxFreq = max(++freq[x], mxFreq);
s[freq[x]].push(x);
}
int pop() {
int val = s[mxFreq].top();
s[mxFreq].pop();
if(s[freq[val]--].empty()) mxFreq--;
return val;
}
};
/**
* Your FreqStack object will be instantiated and called as such:
* FreqStack* obj = new FreqStack();
* obj->push(x);
* int param_2 = obj->pop();
*/
/**
* Your FreqStack object will be instantiated and called as such:
* FreqStack* obj = new FreqStack();
* obj->push(x);
* int param_2 = obj->pop();
*/
/**
* Your FreqStack object will be instantiated and called as such:
* FreqStack* obj = new FreqStack();
* obj->push(x);
* int param_2 = obj->pop();
*/
int main() {
}