Skip to content

Commit 2bad6aa

Browse files
committed
review
1 parent 0dffb0a commit 2bad6aa

32 files changed

+464
-235
lines changed

Java/3Sum Smaller.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
```
1010
/*
11-
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.
11+
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n
12+
that satisfy the condition nums[i] + nums[j] + nums[k] < target.
1213
1314
For example, given nums = [-2, 0, 1, 3], and target = 2.
1415
@@ -38,13 +39,10 @@ public int threeSumSmaller(int[] nums, int target) {
3839
}
3940
Arrays.sort(nums);
4041
int count = 0;
41-
int start, end;
42-
for (int i = 0; i < nums.length - 2; i++) {
43-
int firstNum = nums[i];
44-
start = i + 1;
45-
end = nums.length - 1;
42+
for (int i = 0; i < nums.length - 2; i++) { // first num, num[i] is the fixed item
43+
int start = i + 1, end = nums.length - 1; // move start, end
4644
while (start < end) {
47-
if (nums[i] + nums[start] + nums[end]< target) {
45+
if (nums[i] + nums[start] + nums[end] < target) {
4846
count += end - start;
4947
start++;
5048
} else {

Java/3Sum.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
1516689562
33
tags: Array, Two Pointers
44

5-
方法1:
6-
sort array, for loop + two pointer. O(n)
7-
处理duplicate wthin triplets:
8-
如果最外圈的移动点i重复, 一直顺到结尾的最后一个再用.
9-
如果是triplet内有重复, 用完start point, 移动到结尾.
5+
6+
#### sort array, for loop + two pointer. O(n^2)
7+
- 处理duplicate wthin triplets:
8+
- 如果最外圈的移动点i重复, 一直顺到结尾的最后一个再用.
9+
- 如果是triplet内有重复, 用完start point, 移动到结尾.
1010

1111
Previous notes:
1212
注意:
@@ -52,7 +52,7 @@ Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤
5252
*/
5353
class Solution {
5454
public List<List<Integer>> threeSum(int[] nums) {
55-
final List<List<Integer>> result = new ArrayList<>();
55+
List<List<Integer>> result = new ArrayList<>();
5656
if (nums == null || nums.length == 0) {
5757
return result;
5858
}
@@ -65,8 +65,7 @@ public List<List<Integer>> threeSum(int[] nums) {
6565
int end = i - 1;
6666
while (start < end) {
6767
if (nums[start] + nums[end] + nums[i] == 0) {
68-
final Integer[] intarr = {nums[start], nums[end], nums[i]};
69-
result.add(Arrays.asList(intarr));
68+
result.add(Arrays.asList(nums[start], nums[end], nums[i]));
7069
start++;
7170
while (start < end && nums[start - 1] == nums[start]) { // skip duplicates
7271
start++;

Java/Accounts Merge.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99

1010
#### Union Find
11-
- 构建 Map<email, email parent>, 然后再反向整合: parent -> list of email
11+
- 构建 `Map<email, email parent>`, 然后再反向整合: parent -> list of email
12+
- init with <email, email> for all emails
13+
- 因为不同account可能串email, 那么把所有email union的时候, 不同account 的email也会被串起来
14+
- 最终: 所有的email都被union起来, 指向一个各自union的 parent email
15+
- UnionFind parent map 可以反向输出所有 child under parent.
16+
- 同时要维护一个 <email -> account name> 的map, 最终用来输出.
1217

1318
#### Hash Table solution, passed but very slow
1419
- Definitely need iterate over accounts: merge them by email.
@@ -127,7 +132,7 @@ public List<List<String>> accountsMerge(List<List<String>> accounts) {
127132

128133
for (List<String> account : accounts) {
129134
for (int i = 1; i < account.size() - 1; i++) {
130-
union(account.get(i), account.get(i + 1));
135+
union(account.get(i), account.get(i + 1)); // email across acounts will be auto-merged/linked in unionFind
131136
}
132137
}
133138

Java/Combination Sum.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
- at each level dfs, we have the index as starting point:
2121
- if we are at `index=0, we can have n child dfs() options via for loop`;
2222
- if at `index=1, we will have (n-1) dfs options via for loop`.
23-
- Consider it as the pick/not-pick proble, where the difference is you can pick `x` times at each index rather than only 2 times.
23+
- Consider it as the `pick/not-pick` problem, where the difference is you can pick `x` times at each index rather than only 2 times.
2424
- Overall, we will multiply the # of possibilities: n * (n - 1) * (n - 2) ... * 1 = n! => `O(n!)`
2525

2626
##### Combination DFS 思想
27-
- 在每个index上面都要面临: pick/not pick的选择
27+
- 在每个index上面都要面临: `pick/not pick的选择`, 用for loop over index + backtracking 实现 picks.
2828
- 每次pick以后, 就生成一条新的routine, from this index
2929
- 下一个level的dfs从这个index开始, 对后面(或者当下/if allow index reuse) 进行同样的 pick/not pick 的选择
3030
- 注意1: 每个level dfs 里面, for loop 里会有 end condition: 就不必要dfs下去了.

Java/Construct Binary Tree from Preorder and Inorder Traversal.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
- 跟Convert Sorted Array to Binary Tree类似, 找到对应的index, 然后:
1111
- node.left = dfs(...), node.right = dfs(...)
1212
- Divide and Conquer
13-
- optimize on finding mid node: given value, find mid of inorder. Instead of searching linearly, just store map <value -> index>, O(1)
13+
- optimize on finding `mid node`: given value, find mid of inorder:
14+
- Instead of searching linearly, just store inorder sequence in `map <value -> index>`, O(1)
15+
- IMPORATANT: the mid from inorder sequence will become the main baseline to tell range:
16+
- `range of subTree = (mid - inStart)`
1417
- sapce: O(n), time: O(n) access
1518

1619
```

Java/Flatten Binary Tree to Linked List.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
#### DFS
88
- 分析题意后, 按照题意: Flatten the tree, no extra space.
9-
1. reserve right child
10-
2. DFS flatten部分
11-
3. 移花接木
12-
4. flatten 剩下的.
9+
- 1. reserve right child: `reservedRightNode`
10+
- 2. Connect `root.right = root.left`, DFS flatten(root.right)
11+
- 3. 移花接木, coneect end of list -> reservedRightNode
12+
- 4. flatten 剩下的. root.right...
1313

1414
##### 注意
1515
- 顺序一定要清楚, 不能写错, 写几个example可以看出来
@@ -88,7 +88,7 @@ public void flatten(TreeNode root) {
8888
while (node.right != null) {
8989
node = node.right;
9090
}
91-
node.right = rightNode;
91+
node.right = rightNode; // connect
9292
flatten(root.right);
9393
}
9494
}

Java/Generate Parentheses.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
*/
4949
//Faster because StringBuffer object is reused (add/remove elements of it)
5050
class Solution {
51-
private final static String LEFT = "(";
52-
private final static String RIGHT = ")";
51+
private String LEFT = "(";
52+
private String RIGHT = ")";
5353
public List<String> generateParenthesis(int n) {
5454
List<String> result = new ArrayList<>();
5555
if (n == 0) {

Java/Recover Binary Search Tree.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#### Observation
88
- BST inorder traversal should give small -> large sequence
99
- misplaced means: a **large**->small item would occur, and later a large>**small** would occur.
10-
- The first large && second small item are the 2 candidates.
10+
- The first large && second small item are the 2 candidates. Example
11+
- [1, 5, 7, 10, 12, 15, 18]
12+
- [1, 5, `15, 10`, `12, 7`, 18]
1113

1214
#### dfs, O(1) extra space
1315
- traverse, and take note of the candidate

Java/Strobogrammatic Number II.java

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
M
22
1528732676
3-
tags: Math, DFS, Sequence DFS
3+
tags: Math, DFS, Sequence DFS, Enumeration
44

55
TODO:
66
1. use list, iterative? keep candidates and populating
77
2. clean up the dfs code, a bit messy
88
3. edge case of "0001000" is invalid, right?
99

1010
#### DFS
11+
- A bit like BFS solution: find inner list, and then combine with outter left/right sides.
1112
- find all solutions, DFS will be easier to write than iterative/BFS
1213
- when n = 1, there can be list of candidates at bottom of the tree, so bottom->up is better
1314
- bottom->up, dfs till leaf level, and return candidates.
@@ -34,6 +35,29 @@
3435
3536
*/
3637

38+
class Solution {
39+
List<String> singleDigitList = new ArrayList<>(Arrays.asList("0", "1", "8"));
40+
char[][] digitPair = {{'0', '0'}, {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
41+
public List<String> findStrobogrammatic(int n) {
42+
return dfs(n, n);
43+
}
44+
45+
public List<String> dfs(int n, int max) {
46+
if (n <= 0) return new ArrayList<String>(Arrays.asList(""));
47+
if (n == 1) return singleDigitList;
48+
49+
List<String> subList = dfs(n - 2, max);
50+
List<String> list = new ArrayList<>();
51+
for (String str : subList) {
52+
if (n != max) list.add("0" + str + "0");
53+
for (int i = 1; i < digitPair.length; i++) {
54+
list.add(digitPair[i][0] + str + digitPair[i][1]);
55+
}
56+
}
57+
return list;
58+
}
59+
}
60+
3761
/*
3862
Thoughts:
3963
The items will be in three pattern:
@@ -44,9 +68,9 @@
4468
Recursion untill n reaches 1
4569
*/
4670
class Solution {
47-
private final List<String> singleDigitList = new ArrayList<>(Arrays.asList("0", "1", "8"));
48-
private final List<String> doubleDigitList = new ArrayList<>(Arrays.asList("00", "11", "88", "69", "96"));
49-
private final char[][] digitPair = {{'0', '0'}, {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
71+
List<String> singleDigitList = new ArrayList<>(Arrays.asList("0", "1", "8"));
72+
List<String> doubleDigitList = new ArrayList<>(Arrays.asList("00", "11", "88", "69", "96"));
73+
char[][] digitPair = {{'0', '0'}, {'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}};
5074
public List<String> findStrobogrammatic(int n) {
5175
List<String> result = dfs(n);
5276
for (int i = 0; i < result.size(); i++) {
@@ -61,15 +85,11 @@ public List<String> findStrobogrammatic(int n) {
6185

6286
public List<String> dfs(int n) {
6387
List<String> list = new ArrayList<>();
64-
if (n <= 0) {
65-
return list;
66-
}
67-
if (n == 1) {
68-
return singleDigitList;
69-
} else if (n == 2) {
70-
return doubleDigitList;
71-
}
72-
final List<String> subList = dfs(n - 2);
88+
if (n <= 0) return list;
89+
if (n == 1) return singleDigitList;
90+
if (n == 2) return doubleDigitList;
91+
92+
List<String> subList = dfs(n - 2);
7393
for (String str : subList) {
7494
for (int i = 0; i < digitPair.length; i++) {
7595
list.add(digitPair[i][0] + str + digitPair[i][1]);

Java/Validate Binary Search Tree.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
如题, 验证是否是BST.
66

77
#### DFS
8-
- 查看每个parent-child关系: leftchild < root < rightChild
8+
- 查看每个parent-child关系: leftchild < root < rightChild;
9+
- BST 有两个极端: left-most-leaf is the smallest element, and right-most-leaf is largest
10+
- imagine we know the two extreme border: Integer.MIN_VALUE, Integer.MAX_VALUE; pass node around and compare node vs. node.parent.
911
- 方法: 把root.val 传下来作为 max 或者 min, 然后检查children
12+
-
1013

1114
##### Note:
1215
- min/max需要时long type.

0 commit comments

Comments
 (0)