Skip to content

Commit 5fe91ae

Browse files
committed
commit changes to gh-page aug 13
1 parent 6b9525d commit 5fe91ae

File tree

23 files changed

+422
-60
lines changed

23 files changed

+422
-60
lines changed
Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,83 @@
1+
## What will the maximum path look like
2+
3+
Though paths can be starting from any node and end at any node, the appearances only can be:
4+
5+
* Appearance 1
6+
7+
```
8+
*
9+
/ \
10+
* *
11+
/ \
12+
* *
13+
/ \
14+
S *
15+
\
16+
E
17+
```
18+
19+
* Appearance 2
20+
21+
```
22+
E
23+
/
24+
*
25+
/
26+
*
27+
/
28+
S
29+
```
30+
31+
* Appearance 3
32+
33+
```
34+
S
35+
\
36+
*
37+
\
38+
*
39+
\
40+
*
41+
\
42+
E
43+
```
44+
45+
### Note
46+
47+
Appearances above are only talking about `TRUNK`.
48+
49+
Treat the path below as Appearance 1
50+
51+
```
52+
*
53+
/ \
54+
* *
55+
/ \
56+
* *
57+
\ /
58+
S *
59+
\
60+
E
61+
```
62+
63+
64+
## Tree Version of [Maximum Subarray](../maximum-subarray)
65+
66+
* Appearance 2/3
67+
68+
These two appearances are easy to understand and to build a path, just take the maximum node.
69+
like `Maximum Subarray`, if the sum goes below `0`, forget the old nodes.
70+
71+
* Appearance 1
72+
73+
If a node's left child or right child is less than `0`, drop it, then the node becomes `Appearance 2/3`.
74+
75+
If both the left child and right child are positive, just add them together, `maxPathSum(node.left) + node.val + maxPathSum(node.right)` will be the `maxPathSum` which contains that node.
76+
77+
78+
## Put them together
79+
80+
As we know how to find the `maxPathSum` of any node, we can go through the tree and update the max value.
81+
182

2-
## TODO
3-
* write down thinking
483

_includes/_root/combination-sum-ii/Solution.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
public class Solution {
2-
32
int target;
43

54
int[] candidates;
65

76
int[] stack;
87

9-
ArrayList<ArrayList<Integer>> rt;
8+
ArrayList<List<Integer>> rt;
109

1110
HashSet<String> block;
1211

@@ -42,19 +41,17 @@ void search(int sp, int cur){
4241
stack[sp] = 0;
4342
}
4443

45-
}
44+
}
4645

47-
public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
48-
// Note: The Solution object is instantiated only once and is reused by each test case.
49-
46+
public List<List<Integer>> combinationSum2(int[] num, int target) {
5047
Arrays.sort(num);
5148

5249
candidates = num;
5350
this.target = target;
5451

5552
stack = new int[candidates.length];
5653

57-
rt = new ArrayList<ArrayList<Integer>>();
54+
rt = new ArrayList<List<Integer>>();
5855

5956
block = new HashSet<String>();
6057

@@ -63,4 +60,4 @@ public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
6360
return rt;
6461

6562
}
66-
}
63+
}
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
## Same as [Construct Binary Tree from Preorder and Inorder Traversal](../construct-binary-tree-from-preorder-and-inorder-traversal)
12

2-
## TODO
3-
* write down thinking
3+
The difference is run from end to 0 instead of run from 0 to end.
44

5+
In additional, we should contruct the right tree before the left one.
6+
7+
```
8+
Pre-order: A, C, E, D, B, H, I, G, F
9+
<- p
10+
11+
In-order: A, B, C, D, E, F, G, H, I
12+
q
13+
14+
```
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1+
## Recursion
12

2-
## TODO
3-
* write down thinking
3+
```
4+
Pre-order: F, B, A, D, C, E, G, I, H
5+
p
6+
7+
In-order: A, B, C, D, E, F, G, H, I
8+
q
49
10+
```
11+
12+
From first char `F` in `Pre-order`, we are sure that the root of the tree is `F`.
13+
14+
and from `In-order`, `F` divide the tree into two parts, the left one is `A, B, C, D, E`, the right one is `G, H, I`.
15+
16+
```
17+
F
18+
/ \
19+
A, B, C, D, E G, H, I
20+
21+
```
22+
23+
`A, B, C, D, E` and `B, A, D, C, E` become a new `buildTree` problem. so we can build the tree recursively.
24+
the result tree is the left children of `F`.
25+
26+
27+
Similarly, `G, H, I` and `G, I, H` become the right children of `F`.
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1+
## Recursion
12

2-
## TODO
3-
* write down thinking
3+
### `generateParenthesis(n - i)` and `generateParenthesis(i)`
44

5+
This problem is a bit like [Unique Binary Search Trees II](../unique-binary-search-trees-ii).
6+
7+
Put left and right generateParenthesis together is OK.
8+
9+
* `generateParenthesis(n - i)` + `generateParenthesis(i)`
10+
* `generateParenthesis(i)` + `generateParenthesis(n - i)`
11+
12+
13+
### Special `i = 1`: `generateParenthesis(n - 1)` and `generateParenthesis(1)`
14+
15+
Another new way to generate parenthesis.
16+
17+
* Surrounding :`(` + `generateParenthesis(n)` + `)`
18+
19+
Same as `generateParenthesis(n - i)` and `generateParenthesis(i)`
20+
21+
* `generateParenthesis(n - 1)` + `()`
22+
* `()` + `generateParenthesis(n - 1)`

_includes/_root/generate-parentheses/Solution.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ public List<String> generateParenthesis(int n) {
66

77
HashSet<String> temp = new HashSet<String>();
88

9-
for(int i = 1; i < n ; i++){
9+
for(String s : generateParenthesis(n - 1)){
10+
temp.add("(" + s + ")");
11+
temp.add("()" + s);
12+
temp.add(s + "()");
13+
}
14+
15+
for(int i = 2; i < n - 1 ; i++){
1016
for(String s : generateParenthesis(n - i)){
11-
if(i == 1) temp.add("(" + s + ")");
12-
1317
for(String ss : generateParenthesis(i)){
14-
18+
1519
temp.add(ss + s);
1620
temp.add(s + ss);
1721
}
18-
22+
1923
}
2024
}
2125

2226
return new ArrayList<String>(temp);
23-
2427
}
2528
}
Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,74 @@
1+
## Counting while [Valid Parentheses](../valid-parentheses)
12

2-
## TODO
3-
* write down thinking
3+
When a pair of valid parentheses is found, just add `2` to count.
4+
5+
However, the counting method is a bit tricky. Show you using animation
6+
7+
### Animation
8+
9+
This method adds a counting stack.
10+
11+
```
12+
input [ ( ( ) ( ) ) ]
13+
14+
count []
15+
stack []
16+
17+
i
18+
19+
```
20+
21+
No match put `)` and `(` into stack
22+
23+
```
24+
input [ ) ( ) ) ]
25+
26+
count [ 0 0 ]
27+
stack [ ( ( ]
28+
29+
i
30+
31+
```
32+
33+
34+
35+
found matches pop stack and `count[1] = count[1] + 2`
36+
37+
```
38+
input [ ( ) ) ]
39+
40+
count [ 0 2 ]
41+
stack [ ( ]
42+
43+
i
44+
45+
```
46+
47+
just two steps.
48+
49+
found another matches `(` and `)` pop stack and `count[1] = count[1] + 2`
50+
51+
```
52+
input [ ]
53+
54+
count [ 0 4 ]
55+
stack [ ]
56+
57+
i
58+
59+
```
60+
61+
62+
found matches, but, this time, `count[0] = count[0] + 2` is not enough.
63+
64+
It should be `count[0] = count[0] + 2 + count[1]`, because that here `0` matches parentheses indicates that the current parentheses is enclosing the `1..current`.
65+
66+
67+
Generally, when meets a match,
68+
69+
```
70+
count[stack.size()] = count[stack.size()] + 2 + count[stack.size() + 1]
71+
```
72+
73+
then reset the `count[stack.size() + 1]` to `0`.
474

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1+
## Recursion
12

2-
## TODO
3-
* write down thinking
3+
connecting left and right child is easy, just `node.left.next = node.right`
44

5+
6+
Before
7+
8+
```
9+
1
10+
/ \
11+
2 3
12+
/ \ / \
13+
4 5 6 7
14+
```
15+
16+
17+
After
18+
19+
```
20+
1
21+
/ \
22+
2 -> 3
23+
/ \ / \
24+
4-> 5 6->7
25+
```
26+
27+
`5` and `6` shoud be connected。
28+
29+
when connecting `4` and `5`, `2` and `3` is already connected, that is a good news, we can connect `5` and `6` using `2.next`.
30+
31+
```
32+
node.right.next = node.next.left;
33+
```

0 commit comments

Comments
 (0)