-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossTrainingI.java
390 lines (353 loc) · 11.4 KB
/
CrossTrainingI.java
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
import java.util.*;
import utils.TreeNode;
import utils.TreeNodeP;
import utils.KnaryTreeNode;
public class CrossTrainingI {
public static void main(String[] args) {
String[] input = new String[] {
"4","3","8","1","#","6","#","#","2","5","7"
};
TreeNode root = TreeNode.fromLevelOrder(input);
CrossTrainingI c = new CrossTrainingI();
double target = -1000.0;
int k = 3;
int[] result = c.closestKValues(root, target, k);
System.out.println(result);
}
/**
* Array Deduplication I
* <p>
* Given a sorted integer array, remove duplicate elements. For each group of
* elements with the same value keep only one of them. Do this in-place, using
* the left side of the original array and maintain the relative order of the
* elements of the array. Return the array after deduplication.
*/
public int[] dedup(int[] array) {
int i = 0;
int j = 0;
while (j < array.length) {
if (i == 0 || array[i - 1] != array[j]) {
array[i++] = array[j++];
} else {
while (j < array.length && array[i - 1] == array[j]) {
j++;
}
}
}
return Arrays.copyOf(array, i);
}
/**
* Array Deduplication II
* <p>
* Given a sorted integer array, remove duplicate elements. For each group of
* elements with the same value keep at most two of them. Do this in-place,
* using the left side of the original array and maintain the relative order of
* the elements of the array. Return the array after deduplication.
*/
public int[] dedup2(int[] array) {
int i = 0;
int j = 0;
while (j < array.length) {
if (i == 0 || i == 1 || array[i - 2] != array[j]) {
array[i++] = array[j++];
} else {
while (j < array.length && array[i - 2] == array[j]) {
j++;
}
}
}
return Arrays.copyOf(array, i);
}
/**
* Array Deduplication III
* <p>
* Given a sorted integer array, remove duplicate elements. For each group of
* elements with the same value do not keep any of them. Do this in-place, using
* the left side of the original array and and maintain the relative order of
* the elements of the array. Return the array after deduplication.
*/
public int[] dedup3(int[] array) {
int i = 0;
int j = 0;
while (j < array.length) {
if (j == array.length - 1 || array[j] != array[j + 1]) {
array[i++] = array[j++];
} else {
int cur = array[j];
while (j < array.length && array[j] == cur) {
j++;
}
}
}
return Arrays.copyOf(array, i);
}
/**
* Move 0s To The End II
* <p>
* Given an array of integers, move all the 0s to the right end of the array.
* The relative order of the elements in the original array need to be
* maintained.
*/
public int[] moveZero(int[] array) {
int i = 0;
int j = 0;
while (j < array.length) {
if (array[j] != 0) {
swap(array, i++, j++);
} else {
j++;
}
}
return array;
}
private void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
/**
* Rotate Matrix
* <p>
* Rotate an N * N matrix clockwise 90 degrees.
*/
public void rotate(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n / 2; i++) {
for (int j = i; j < n - 1 - i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - j - 1][i];
matrix[n - j - 1][i] = matrix[n - 1 - i][n - j - 1];
matrix[n - 1 - i][n - j - 1] = matrix[j][n - 1 - i];
matrix[j][n - 1 - i] = temp;
}
}
}
/**
* Get Keys In Binary Tree Layer By Layer Zig-Zag Order
* <p>
* Get the list of keys in a given binary tree layer by layer in zig-zag order.
*/
public List<Integer> zigZag(TreeNode root) {
Deque<TreeNode> deque = new ArrayDeque<>();
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
deque.offerLast(root);
boolean l2r = false;
while (!deque.isEmpty()) {
if (l2r) {
int size = deque.size();
while (size-- > 0) {
TreeNode node = deque.pollFirst();
result.add(node.key);
if (node.left != null) {
deque.offerLast(node.left);
}
if (node.right != null) {
deque.offerLast(node.right);
}
}
} else {
int size = deque.size();
while (size-- > 0) {
TreeNode node = deque.pollLast();
result.add(node.key);
if (node.right != null) {
deque.offerFirst(node.right);
}
if (node.left != null) {
deque.offerFirst(node.left);
}
}
}
l2r = !l2r;
}
return result;
}
/**
* Lowest Common Ancestor Binary Search Tree I
* <p>
* Given two keys in a binary search tree, find their lowest common ancestor.
*/
public TreeNode lca(TreeNode root, int p, int q) {
if (p > q) {
int temp = p;
p = q;
q = temp;
}
while (root.key > q || root.key < p) {
if (root.key < p) {
root = root.right;
} else {
root = root.left;
}
}
return root;
}
/**
* Lowest Common Ancestor II
* <p>
* Given two nodes in a binary tree (with parent pointer available), find their
* lowest common ancestor.
*/
public TreeNodeP lowestCommonAncestor(TreeNodeP one, TreeNodeP two) {
TreeNodeP cur1 = one;
TreeNodeP cur2 = two;
int height1 = 0;
int height2 = 0;
while (cur1.parent != null) {
height1++;
cur1 = cur1.parent;
}
while (cur2.parent != null) {
height2++;
cur2 = cur2.parent;
}
if (height1 < height2) {
int h = height2 - height1;
while (h-- > 0) {
two = two.parent;
}
} else {
int h = height1 - height2;
while (h-- > 0) {
one = one.parent;
}
}
while (one != two) {
one = one.parent;
two = two.parent;
}
return one;
}
/**
* Array Deduplication IV
* <p>
* Given an unsorted integer array, remove adjacent duplicate elements
* repeatedly, from left to right. For each group of elements with the same
* value do not keep any of them. Do this in-place, using the left side of the
* original array. Return the array after deduplication.
*/
public int[] dedup4(int[] array) {
int i = 0;
int j = 0;
while (j < array.length) {
if (i == 0 || array[i - 1] != array[j]) {
array[i++] = array[j++];
} else {
while (j < array.length && array[i - 1] == array[j]) {
j++;
}
i--;
}
}
return Arrays.copyOf(array, i);
}
/**
* Closest Number In Binary Search Tree II
* <p>
* In a binary search tree, find k nodes containing the closest numbers to the
* given target number. return them in sorted array
*/
public int[] closestKValues(TreeNode root, double target, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return (int) (Math.abs(b - target) - Math.abs(a - target));
}
});
closestKValues(root, pq, k, target);
k = Math.min(pq.size(), k);
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = pq.poll();
}
Arrays.sort(result);
return result;
}
private void closestKValues(TreeNode node, PriorityQueue<Integer> pq, int k, double target) {
if (node == null) {
return;
}
int key = node.key;
if (pq.size() < k) {
pq.offer(key);
} else if (Math.abs(key - target) < Math.abs(pq.peek() - target)) {
pq.poll();
pq.offer(key);
}
closestKValues(node.left, pq, k, target);
closestKValues(node.right, pq, k, target);
}
/**
* Lowest Common Ancestor IV
* <p>
* Given K nodes in a binary tree, find their lowest common ancestor.
*/
public TreeNode lowestCommonAncestor2(TreeNode root, List<TreeNode> nodes) {
Set<TreeNode> set = new HashSet<>();
for (TreeNode n : nodes) {
set.add(n);
}
return lowestCommonAncestor(root, set);
}
private TreeNode lowestCommonAncestor(TreeNode node, Set<TreeNode> set) {
if (node == null || set.contains(node)) {
return node;
}
TreeNode left = lowestCommonAncestor(node.left, set);
TreeNode right = lowestCommonAncestor(node.right, set);
if (left != null && right != null) {
return node;
} else {
return left == null ? right : left;
}
}
/**
* Lowest Common Ancestor V
* <p>
* Given two nodes in a K-nary tree, find their lowest common ancestor.
*/
public KnaryTreeNode lowestCommonAncestor3(KnaryTreeNode root, KnaryTreeNode a, KnaryTreeNode b) {
if (root == null || root == a || root == b) {
return root;
}
KnaryTreeNode prev = null;
for (KnaryTreeNode node : root.children) {
if (prev == null) {
prev = lowestCommonAncestor3(node, a, b);
} else {
if (lowestCommonAncestor3(node, a, b) != null) {
return root;
}
}
}
return prev;
}
/**
* Lowest Common Ancestor VI
* <p>
* Given M nodes in a K-nary tree, find their lowest common ancestor.
*/
public KnaryTreeNode lowestCommonAncestor4(KnaryTreeNode root, List<KnaryTreeNode> nodes) {
Set<KnaryTreeNode> set = new HashSet<>(nodes);
return lowestCommonAncestor4(root, set);
}
private KnaryTreeNode lowestCommonAncestor4(KnaryTreeNode root, Set<KnaryTreeNode> set) {
if (root == null || set.contains(root)) {
return root;
}
KnaryTreeNode prev = null;
for (KnaryTreeNode node : root.children) {
if (prev == null) {
prev = lowestCommonAncestor4(node, set);
} else {
if (lowestCommonAncestor4(node, set) != null) {
return root;
}
}
}
return prev;
}
}