-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFSII.java
370 lines (330 loc) · 12.2 KB
/
DFSII.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
import java.util.*;
public class DFSII {
public static void main(String[] args) {
DFSII d = new DFSII();
// List<String> a = d.subSets("abc");
// for (String b : a) {
// System.out.print(b + ",");
// }
System.out.println(d.validParenthesesIII(1, 2, 1));
}
/**
* All SubSets II
* <p>
* Given a set of characters represented by a String, return a list containing
* all subsets of the characters. Notice that each subset returned will be
* sorted to remove the sequence.
*/
public List<String> subSets(String set) {
List<String> result = new ArrayList<>();
if (set == null) {
return result;
}
StringBuilder sb = new StringBuilder();
int[] charSet = new int[26];
for (int i = 0; i < set.length(); i++) {
charSet[set.charAt(i) - 'a']++;
}
result.add("");
subSets(charSet, 0, result, sb);
return result;
}
private void subSets(int[] charSet, int index, List<String> result, StringBuilder sb) {
if (index == 26) {
return;
}
int k = charSet[index];
subSets(charSet, index + 1, result, sb);
for (int i = 0; i < k; i++) {
sb.append((char) (index + 'a'));
result.add(sb.toString());
subSets(charSet, index + 1, result, sb);
}
sb.delete(sb.length() - k, sb.length());
}
/**
* All Subsets of Size K
* <p>
* Given a set of characters represented by a String, return a list containing
* all subsets of the characters whose size is K.
*/
public List<String> subSetsOfSizeK(String set, int k) {
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
char[] ch = set.toCharArray();
subSetsOfSizeK(ch, 0, k, result, sb);
return result;
}
private void subSetsOfSizeK(char[] ch, int index, int k, List<String> result, StringBuilder sb) {
if (sb.length() == k) {
result.add(sb.toString());
return;
}
if (index == ch.length) {
return;
}
sb.append(ch[index]);
subSetsOfSizeK(ch, index + 1, k, result, sb);
sb.deleteCharAt(sb.length() - 1);
subSetsOfSizeK(ch, index + 1, k, result, sb);
}
/**
* All Valid Permutations Of Parentheses II
* <p>
* Get all valid permutations of l pairs of (), m pairs of <> and n pairs of {}.
*/
public List<String> validParentheses(int l, int m, int n) {
List<String> result = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder();
validParentheses(0, 0, l, 0, 0, m, 0, 0, n, stack, sb, result);
return result;
}
private void validParentheses(int lL, int lR, int l, int mL, int mR, int m, int nL, int nR, int n,
Deque<Integer> stack, StringBuilder sb, List<String> result) {
if (lL == l && lR == l && mL == m && mR == m && nL == n && nR == n) {
result.add(sb.toString());
return;
}
if (!stack.isEmpty()) {
int cur = stack.peekLast();
stack.pollLast();
if (cur == 0) {
sb.append(")");
validParentheses(lL, lR + 1, l, mL, mR, m, nL, nR, n, stack, sb, result);
stack.offerLast(0);
} else if (cur == 1) {
sb.append(">");
validParentheses(lL, lR, l, mL, mR + 1, m, nL, nR, n, stack, sb, result);
stack.offerLast(1);
} else {
sb.append("}");
validParentheses(lL, lR, l, mL, mR, m, nL, nR + 1, n, stack, sb, result);
stack.offerLast(2);
}
sb.deleteCharAt(sb.length() - 1);
}
if (lL < l) {
stack.offerLast(0);
sb.append("(");
validParentheses(lL + 1, lR, l, mL, mR, m, nL, nR, n, stack, sb, result);
sb.deleteCharAt(sb.length() - 1);
stack.pollLast();
}
if (mL < m) {
stack.offerLast(1);
sb.append("<");
validParentheses(lL, lR, l, mL + 1, mR, m, nL, nR, n, stack, sb, result);
sb.deleteCharAt(sb.length() - 1);
stack.pollLast();
}
if (nL < n) {
stack.offerLast(0);
sb.append("{");
validParentheses(lL, lR, l, mL, mR, m, nL + 1, nR, n, stack, sb, result);
sb.deleteCharAt(sb.length() - 1);
stack.pollLast();
}
}
/**
* Factor Combinations
* <p>
* Given an integer number, return all possible combinations of the factors that
* can multiply to the target number.
*/
public List<List<Integer>> combinations(int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
combinations(target, cur, result);
return result;
}
private void combinations(int target, List<Integer> cur, List<List<Integer>> result) {
if (target == 1 && cur.size() > 1) {
result.add(new ArrayList<>(cur));
return;
}
for (int i = 2; i <= target; i++) {
if (target % i == 0) {
if (cur.isEmpty() || cur.get(cur.size() - 1) <= i) {
cur.add(i);
combinations(target / i, cur, result);
cur.remove(cur.size() - 1);
}
}
}
}
/**
* All Permutations of Subsets
* <p>
* Given a string with no duplicate characters, return a list with all
* permutations of the string and all its subsets.
*/
public List<String> allPermutationsOfSubsets(String set) {
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
char[] arr = set.toCharArray();
allPermutationsOfSubsets(arr, 0, sb, result);
return result;
}
private void allPermutationsOfSubsets(char[] arr, int index, StringBuilder sb, List<String> result) {
result.add(sb.toString());
if (index == arr.length) {
return;
}
for (int i = index; i < arr.length; i++) {
swap(arr, index, i);
sb.append(arr[index]);
allPermutationsOfSubsets(arr, index + 1, sb, result);
sb.deleteCharAt(sb.length() - 1);
swap(arr, index, i);
}
}
private void swap(char[] arr, int i, int j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* Two Subsets With Min Difference
* <p>
* Given a set of n integers, divide the set in two subsets of n/2 sizes each
* such that the difference of the sum of two subsets is as minimum as possible.
* Return the minimum difference(absolute value).
*/
public int minDifference(int[] array) {
int sum = 0;
for (int num : array) {
sum += num;
}
int[] closest = new int[] { Integer.MAX_VALUE };
minDifference(array, 0, 0, 0, sum, closest);
return closest[0];
}
private void minDifference(int[] array, int index, int size, int curSum, int sum, int[] closest) {
if (size == array.length / 2) {
closest[0] = Math.min(closest[0], Math.abs(sum - 2 * curSum));
return;
}
if (index == array.length) {
return;
}
minDifference(array, index + 1, size + 1, curSum + array[index], sum, closest);
minDifference(array, index + 1, size, curSum, sum, closest);
}
/**
* All SubSets II of Size K
* <p>
* Given a set of characters represented by a String, return a list containing
* all subsets of the characters whose size is K. Notice that each subset
* returned will be sorted for deduplication.
*/
public List<String> subSetsIIOfSizeK(String set, int k) {
char[] arr = set.toCharArray();
Arrays.sort(arr);
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
subSetsIIOfSizeK(arr, 0, k, sb, result);
return result;
}
private void subSetsIIOfSizeK(char[] arr, int index, int k, StringBuilder sb, List<String> result) {
if (sb.length() == k) {
result.add(sb.toString());
return;
}
if (index == arr.length) {
return;
}
int nextIndex = index;
while (nextIndex < arr.length && arr[nextIndex] == arr[index]) {
nextIndex++;
}
subSetsIIOfSizeK(arr, nextIndex, k, sb, result);
for (int i = index; i < nextIndex; i++) {
sb.append(arr[i]);
subSetsIIOfSizeK(arr, nextIndex, k, sb, result);
}
int length = sb.length();
sb.delete(length - nextIndex + index, length);
}
/**
* All Valid Permutations Of Parentheses III
* <p>
* Get all valid permutations of l pairs of (), m pairs of <> and n pairs of {},
* subject to the priority restriction: {} higher than <> higher than ().
*/
public List<String> validParenthesesIII(int l, int m, int n) {
List<String> result = new ArrayList<>();
StringBuilder sb = new StringBuilder();
validParenthesesIII(0, 0, l, 0, 0, m, 0, 0, n, sb, result);
return result;
}
private void validParenthesesIII(int lLeft, int lRight, int l, int mLeft, int mRight, int m, int nLeft, int nRight,
int n, StringBuilder sb, List<String> result) {
if (lLeft == l && lRight == l && mLeft == m && mRight == m && nLeft == n && nRight == n) {
result.add(sb.toString());
return;
}
if (lLeft < l && lLeft == lRight) {
sb.append('(');
validParenthesesIII(lLeft + 1, lRight, l, mLeft, mRight, m, nLeft, nRight, n, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
if (mLeft < m && mLeft == mRight && lLeft == lRight) {
sb.append('<');
validParenthesesIII(lLeft, lRight, l, mLeft + 1, mRight, m, nLeft, nRight, n, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
if (nLeft < n && nLeft == nRight && lLeft - lRight == 0 && mLeft - mRight == 0) {
sb.append('{');
validParenthesesIII(lLeft, lRight, l, mLeft, mRight, m, nLeft + 1, nRight, n, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
if (lLeft > lRight) {
sb.append(')');
validParenthesesIII(lLeft, lRight + 1, l, mLeft, mRight, m, nLeft, nRight, n, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
if (mLeft > mRight && lLeft - lRight == 0) {
sb.append('>');
validParenthesesIII(lLeft, lRight, l, mLeft, mRight + 1, m, nLeft, nRight, n, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
if (nLeft > nRight && lLeft - lRight == 0 && mLeft - mRight == 0) {
sb.append('}');
validParenthesesIII(lLeft, lRight, l, mLeft, mRight, m, nLeft, nRight + 1, n, sb, result);
sb.deleteCharAt(sb.length() - 1);
}
}
/**
* Keep Distance For Identical Elements
* <p>
*
* Given an integer k, arrange the sequence of integers [1, 1, 2, 2, 3, 3, ....,
* k - 1, k - 1, k, k], such that the output integer array satisfy this
* condition:
*
* Between each two i's, they are exactly i integers (for example: between the
* two 1s, there is one number, between the two 2's there are two numbers).
*/
public int[] keepDistance(int k) {
int[] result = new int[2 * k];
return keepDistance(result, k) ? result : null;
}
private boolean keepDistance(int[] result, int n) {
if (n == 0) {
return true;
}
for (int i = 0; i + n + 1 < result.length; i++) {
if (result[i] == 0 && result[i + n + 1] == 0) {
result[i] = n;
result[i + n + 1] = n;
if (keepDistance(result, n - 1)) {
return true;
}
result[i] = 0;
result[i + n + 1] = 0;
}
}
return false;
}
}