-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare
380 lines (330 loc) · 10.2 KB
/
prepare
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
"leetcode".anagramIndexOf("doc") = 4
"leetcode".anagramIndexOf("index") = -1
int findanagramindex(string &s, string &key) {
int n = key.size();
int m = s.size();
if (m == 0) return -1;
string tmp = key;
sort(tmp.begin(), tmp.end());
for (int i = 0; i <= m - n; i++) {
string t = substr(i,n);
sorted(t.begin(), t.end());
if (t == tmp)
return i;
}
return -1;
}
2.
"1.13" + "0.9" => "2.03"
string doubsum(string &a, string &b) {
string res;
if (a.empty() && b.empty())
return res;
int m = a.size();
int n = b.size();
int i = 0, j = 0;
while (i < m && a[i] != '.')
i++;
while (i < n && b[j] != '.')
j++;
string a1 = a.substr(0, i);
string a2 = a.substr(i+1);
string b1 = b.substr(0, j);
string b2 = b.substr(j+1);
string ab1 = addStrings(a1, b1);
string ab2 = addStrings(a2, b2);
return ab1 +"." + ab2;
}
string addStrings(string &num1, string &num2) {
string res = "";
int m = num1.size(), n = num2.size(), i = m - 1, j = n - 1, carry = 0;
while (i >= 0 || j >= 0) {
int a = i >= 0? num1[i--] - '0':0;
int b = j >= 0? num2[j--] - '0':0;
int sum = a + b + carry;
res.insert(res.begin(), sum % 10 + '0');
carry = sum/10;
}
if (carry)
return "1" + res;
else
return res;
}
leetcode 256
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
int res = 0, sum = 0, n = nums.size();
unordered_map<int, int> m{{0, 1}};
for (int i = 0; i < n; ++i) {
sum += nums[i];
res += m[sum - k];
++m[sum];
}
return res;
}
};
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if (root) dfs(root, "", res);
return res;
}
void dfs(TreeNode *root, string out, vector<string> &res) {
out += to_string(root->val);
if (!root->left && !root->right)
res.push_back(out);
else {
if (root->left) dfs(root->left, out + "->", res);
if (root->right) dfs(root->right, out + "->", res);
}
}
void dfs(Graph *root, string out, vector<string> &res, vector<bool> &graphv) {
if (graphv[root])
return;
out += to_string(root->val);
for (auto a:graphv[root]) {
dfs(a, out +"->" res, graphv);
}
}
};
a) phone number combination;
// Recursion
class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if (digits.empty()) return res;
string dict[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
letterCombinationsDFS(digits, dict, 0, "", res);
return res;
}
void letterCombinationsDFS(string digits, string dict[], int level, string out, vector<string> &res) {
if (level == digits.size()) res.push_back(out);
else {
string str = dict[digits[level] - '2'];
for (int i = 0; i < str.size(); ++i) {
out.push_back(str[i]);
letterCombinationsDFS(digits, dict, level + 1, out, res);
out.pop_back();
}
}
}
};
We are given a list schedule of employees, which represents the working time for each employee.
Each employee has a list of non-overlapping Intervals, and these intervals are in sorted order.
Return the list of finite intervals representing common, positive-length free time for all employees, also in sorted order.
/*
Example 1:
Input: schedule = [[[1,2],[5,6]],[[1,3]],[[4,10]]]
Output: [[3,4]]
Explanation:
There are a total of three employees, and all common
free time intervals would be [-inf, 1], [3, 4], [10, inf].
We discard any intervals that contain inf as they aren't finite.
*/
class Solution {
public:
vector<Interval> employeeFreeTime(vector<vector<Interval>>& schedule) {
vector<Interval> res;
map<int, int> m;
int cnt = 0;
for (auto employee : schedule) {
for (Interval i : employee) {
++m[i.start];
--m[i.end];
}
}
for (auto a:m) {
cnt += a.second;
if (!cnt)
res.push_back(Interval(a.first, 0));
if (cnt && !res.empty() && !res.back.end) res.back().end = a.first;
}
if (!res.empty()) res.pop_back();
return res;
}
};
1. 合并区间, 给一个数组,每个元素是一个pair,表示数轴上一个区间,已知区间已按左端点升序排列,要求合并所有有交集的区间。返回的区间集要无交集。.
vector<Interval> mergetwointeval(vector<Interval> &a, vector<Interval> &b) {
vector<Interval> res;
map<int, int> m;
for (auto e:a) {
m[e.start]++;
m[e.end]--;
}
for (auto e:b) {
m[e.start]++;
m[e.end]--;
}
Interval tmp;
int cnt = 0;
bool find = false;
for (auto &e:m) {
cnt += e.second;
if (cnt == 1 && pivot.first != 0) {
pivot.start = e.first;
}else if (cnt = 0 && pivot.end == 0) {
pivot.end = e.second;
res.push_back(pivot);
pivot.reset();
}
}
return;
}
vector<Interval> intersection(vector<Interval>& lhs, vector<Interval>& rhs) {
vector<Interval> res;
map<int, int> m;
for (auto& a : lhs) {
m[a.start]++;
m[a.end]--;
}
for (auto& a : rhs) {
m[a.start]++;
m[a.end]--;
}
Interval pivot;
int cur = 0;
for (auto& a : m) {
cur += a.second;
if (cur == 2) {
pivot.start = a.first;
} else if (pivot.start != pivot.end) {
pivot.end = a.first;
res.push_back(pivot);
pivot.reset();
}
}
return res;
};
b) find union intervals of two list of internals.
4) ML design, news feed rank, 问的比较系统,但是比较开放。ains the anagram of another string.
find lowest lowestCommonAncestor
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (!root || p == root || q == root) return root;
TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p , q);
if (left && right) return root;
return left ? left : right;
}
};
insert and remove in O(1)
class RandomizedSet {
public:
/** Initialize your data structure here. */
RandomizedSet() {}
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
bool insert(int val) {
if (m.count(val)) return false;
nums.push_back(val);
m[val] = nums.size() - 1;
return true;
}
/** Removes a value from the set. Returns true if the set contained the specified element. */
bool remove(int val) {
if (!m.count(val)) return false;
int last = nums.back();
m[last] = m[val];
nums[m[val]] = last;
nums.pop_back();
m.erase(val);
return true;
}
/** Get a random element from the set. */
int getRandom() {
return nums[rand() % nums.size()];
}
private:
vector<int> nums;
unordered_map<int, int> m;
};
给了一个面经没有的 medium 题,一个多叉树,每个节点有三种状态,0,1,2. 要求如果子节点全部为2,父节点也为2. 子节点全部为0,
父节点为0,其余情况都是为1.\给你一个已经合法的树,实现一个 改value 的函数 (node, targetValue),难点在怎么连带 改其他需要 改值的node
public class Solution{
Node root;
public void updateNode(Node* root, int val){
vector<Node*> st;
dfs(n, root, stack);
}
dfs(Node* target, Node* parent, Stack<Node*> stack){
if (parent == null) return;
if(parent == target){
if(target.val == 1){
checkChangeBack(target, stack);
}else{
//update children
updateChildren(target);
//update parent
updateParent(target, stack);
}
return;
}
node->left = parent ->left;
node->right = parent->right;
st.push(parent);
dfs(target, left, stack);
dfs(target, right, stack);
}
void checkChangeBack(Node *target, Stack<Node> stack){
//change back by children
Node* left = target->left;
Node* right = target->right;
if(left != null && right != null){
if((left.val == 0 && right.val == 0) || (left.val == 2 && right.val == 2) ){
target.val = left.val;
}
}
//change back by parent
if(!stack.empty()){
Node parent = stack.peek();
if(parent.val == 0 || parent.val == 2){
target.val = parent.val;
}
}
}
void updateChildren(Node* root){
if(root == null) return;
if(root->val == 0 || root -> val == 2){
Node* left = root->left;
Node* right = root->right;
if (left != null)
left.val = root.val;
if (right != null)
right.val = root.val;
updateChildren(left);
updateChildren(right);
}
}
updateParent(Node target, Stack<Node> stack){
if(target.val == 1) return;
if(st.empty()) return;
Node* parent = stack.top();
st.pop();
Node* sibling = target == parent.left ? parent.right : parent.left;
if(sibling == null) return;
if(target.val == sibling.val) parent.val = target.val;
updateParent(parent, stack);
}
};
Get random point given a list of rectangle.
pair<int, int> getrandom(vector<rect> &rects) {
int sum = 0;
for (auto a:rects)
sum += a.w * a.h;
int randn = rand(sum);
pp point;
int csum = 0;
for (int i = 0; i < rects.size(); i++) {
if (csum < randn)
csum += rects[i];
else {
int area = a.w * a.h;
point.first = rects[i].x + (csum - randn) % area;
point.second = rects[i].y + (csum - randon) % area;
}
}
return point;
}