Skip to content

Commit 50e83db

Browse files
committed
3.17.f
1 parent a121cf2 commit 50e83db

14 files changed

+242
-729
lines changed

Java/Best Time to Buy and Sell Stock IV.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
local[i][j] = max(global[i1][j1] + diff, local[i1][j] + diff)
1111
global[i][j] = max(global[i1][j], local[i][j])
1212

13+
local[i][j]: 第i天当天一定进行第j次交易的profit
14+
global[i][j]: 第i天总共进行了j次交易的profit.
15+
1316
local[i][j]和global[i][j]的区别是local[i][j]意味着在第i天一定有交易卖出发生
1417
当第i天的价格高于第i-1即diff > 0那么可以把这次交易第i-1天买入第i天卖出跟第i-1天的交易卖出合并为一次交易即local[i][j]=local[i-1][j]+diff
1518
当第i天的价格不高于第i-1即diff<=0那么local[i][j]=global[i-1][j-1]+diff而由于diff<=0所以可写成local[i][j]=global[i-1][j-1]。

Java/Clone Graph.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
M
2+
3+
Use HashMap to mark cloned nodes.
4+
5+
先能复制多少Node复制多少然后把neighbor 加上
6+
7+
8+
```
19
/*
210
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
311
@@ -23,7 +31,7 @@
2331
\_/
2432
Hide Tags Depth-first Search Breadth-first Search Graph
2533
26-
34+
2735
*/
2836

2937
/*
@@ -134,3 +142,5 @@ public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
134142
}
135143
}
136144

145+
146+
```

Java/Copy List with Random Pointer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
```
1010
/*
1111
12-
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
12+
A linked list is given such that each node contains an additional random pointer
13+
which could point to any node in the list or null.
1314
1415
Return a deep copy of the list.
1516

Java/Count Primes.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
什么是prime number: >=2的没有除自己和1以外公约数的数
2-
还有另外一个中定义方法
3-
这个n,有没有小于n的一个i,而达到i*i + # of i = n. 如果有那就不是 prime
1+
E
2+
3+
什么是prime number: >=2的没有除自己和1以外公约数的数
4+
5+
还有另外一个定义方法!!
6+
这个n,有没有小于n的一个i,而达到i*i + # of i = n. 如果有那就不是 prime
7+
8+
方法很牛逼也很数学没做的时候可能想不到做了之后就觉得我去有道理啊
9+
简而言之简历一个boolean长条存isPrime[]。 然后从i=2全部变true.
10+
然后利用这个因子的性质非prime满足条件self*self, self * self + self ... etc.
11+
所以就check每一个j, j+i, j+i+i, 然后把所有non-prime全部mark成false.
12+
最后数一遍还剩下的true个数就好了
413

5-
方法很牛逼也很数学没做的时候可能想不到做了之后就觉得我去有道理啊
6-
简而言之简历一个boolean长条存isPrime[]。 然后从i=2全部变true.
7-
然后利用这个因子的性质非prime满足条件self*self, self * self + self ... etc.
8-
所以就check每一个j, j+i, j+i+i, 然后把所有non-prime全部mark成false.
9-
最后数一遍还剩下的true个数就好了
1014
```
1115
/*
1216
Description:
@@ -19,7 +23,8 @@
1923
Attempt2: https://leetcode.com/problems/count-primes/ explains it well
2024
1. Ignore 1 and n. Don't count 1 and the number itself in.
2125
2. Assume all numbers are prime in a boolean[]. Check off those are certainly not prime, the remaining will be prime.
22-
3. For any n, only need to check up to i * i < n; more than that, for example 2 x 6 is same as checking 6x2, but 6x2 is not necessary to check.
26+
3. For any n, only need to check up to i * i < n; more than that,
27+
for example 2 x 6 is same as checking 6x2, but 6x2 is not necessary to check.
2328
4. How to mark things off:
2429
The first non-prime is always i^2: self * self.
2530
Then more non-primes:self * self, self * (self + 1), self * (self + 2) ... etc.

Java/Find the Connected Component in the Undirected Graph.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
BFS遍历把每个node的neighbor都加进来
1+
M
2+
3+
BFS遍历把每个node的neighbor都加进来
24

3-
一定注意要把visit过的node Mark一下因为curr node也会是别人的neighbor会无限循环
5+
一定注意要把visit过的node Mark一下因为curr node也会是别人的neighbor会无限循环
46

5-
Component的定义所有Component内的node必须被串联起来via path (反正这里是undirected, 只要链接上就好)
7+
Component的定义所有Component内的node必须被串联起来via path (反正这里是undirected, 只要链接上就好)
68

7-
这道题其实component在input里面都已经给好了所有能一口气visit到的全部加进queue里面他们就是一个component里面的了
9+
这道题其实component在input里面都已经给好了所有能一口气visit到的全部加进queue里面他们就是一个component里面的了
10+
11+
而我们这里不需要判断他们是不是Component
812

9-
而我们这里不需要判断他们是不是Component
1013
```
1114
/*
1215
Find the number connected component in the undirected graph.

Java/Find the Weak Connected Component in the Directed Graph.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
Identify这是个union-find问题还挺巧妙
2-
看到了weak component的形式一个点指向所有那么所有的点都有一个公共的parent然后就是要找出这些点
1+
M
32

4-
为何不能从一个点出发比如A直接print它所有的neighbors呢
5-
不行如果轮到了B点那因为是directed,它也不知道A的情况也不知道改如何继续加或者下手
3+
Identify这是个union-find问题还挺巧妙
4+
看到了weak component的形式一个点指向所有那么所有的点都有一个公共的parent然后就是要找出这些点
65

7-
所以要把所有跟A有关系的点或者接下去和A的neighbor有关系的点都放进union-find里面让这些点有Common parents.
6+
为何不能从一个点出发比如A直接print它所有的neighbors呢
7+
不行如果轮到了B点那因为是directed,它也不知道A的情况也不知道改如何继续加或者下手
8+
9+
所以要把所有跟A有关系的点或者接下去和A的neighbor有关系的点都放进union-find里面让这些点有Common parents.
10+
11+
最后output的想法
12+
做一个 map <parent ID, list>。
13+
之前我们不是给每个num都存好了parent了嘛
14+
每个num都有个parent, 然后不同的parent就创造一个不同的list
15+
最后把Map里面所有的list拿出来就好了
816

9-
最后output的想法
10-
做一个 map <parent ID, list>。
11-
之前我们不是给每个num都存好了parent了嘛
12-
每个num都有个parent, 然后不同的parent就创造一个不同的list
13-
最后把Map里面所有的list拿出来就好了
1417
```
1518
/*
1619
Find the number Weak Connected Component in the directed graph.

Java/Graph Valid Tree.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
复习Union-Find的另外一个种形式
2-
题目类型查找2个元素是不是在一个set里面如果不在false. 如果在那就合并成一个set,共享parent.
3-
存储的关键都是元素相对的index上存着他的root parent.
1+
M
2+
3+
复习Union-Find的另外一个种形式
4+
题目类型查找2个元素是不是在一个set里面如果不在false. 如果在那就合并成一个set,共享parent.
5+
存储的关键都是元素相对的index上存着他的root parent.
46

57
另一个union-find用hashmap的http://www.lintcode.com/en/problem/find-the-weak-connected-component-in-the-directed-graph/
8+
9+
610
```
711
/*
812
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes),

Java/Minimum Window Substring.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Can you do it in time complexity O(n) ?
3939
/*
4040
03.09.2016
4141
http://blog.sina.com.cn/s/blog_eb52001d0102v2il.html
42+
http://www.rudy-yuan.net/archives/185/
4243
4344
只利用一个hashmap save frequency of target.
4445
1. 从map里面 减去 source char match target char 的frequency. 如果发现frequency = 0, size++

Java/Number of Islands II.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
H
2+
3+
4+
```
15
/*
2-
Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k). Originally, the 2D matrix is all 0 which means there is only sea in the matrix. The list pair has k operator and each operator has two integer A[i].x, A[i].y means that you can change the grid matrix[A[i].x][A[i].y] from sea to island. Return how many island are there in the matrix after each operator.
6+
Given a n,m which means the row and column of the 2D matrix and an array of pair A( size k).
7+
Originally, the 2D matrix is all 0 which means there is only sea in the matrix.
8+
The list pair has k operator and each operator has two integer A[i].x, A[i].y means
9+
that you can change the grid matrix[A[i].x][A[i].y] from sea to island.
10+
11+
Return how many island are there in the matrix after each operator.
12+
313
4-
Have you met this question in a real interview? Yes
514
Example
615
Given n = 3, m = 3, array of pair A = [(0,0),(0,1),(2,2),(2,1)].
716
@@ -108,3 +117,5 @@ public List<Integer> numIslands2(int n, int m, Point[] operators) {
108117
return rst;
109118
}
110119
}
120+
121+
```

Java/Topological Sorting.java

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
M
2+
3+
比较特别的BFS.
4+
5+
几个graph的condition
6+
1. 可能有多个root
7+
2. directed node, 可以direct backwards.
8+
9+
Steps:
10+
Track all neighbors/childrens. 把所有的children都存在map<label, count>里面
11+
先把所有的root加一遍可能多个root并且全部加到queue里面
12+
13+
然后以process queue, do BFS:
14+
Only when map.get(label) == 0, add into queue && rst.
15+
这用map track apperance, 确保在后面出现的node, 一定最后process.
16+
17+
18+
```
119
/*
220
Given an directed graph, a topological order of the graph nodes is defined as follow:
321
@@ -51,40 +69,42 @@ public class Solution {
5169
public ArrayList<DirectedGraphNode> topSort(ArrayList<DirectedGraphNode> graph) {
5270
ArrayList<DirectedGraphNode> rst = new ArrayList<DirectedGraphNode>();
5371
if (graph == null || graph.size() == 0) {
54-
return graph;
72+
return graph;
5573
}
56-
//Keep track of all neighbors in HashMap
74+
//Keep track of all neighbors in HashMap
5775
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
5876
for (DirectedGraphNode node : graph) {
59-
for (DirectedGraphNode neighbor : node.neighbors) {
60-
int keyN = neighbor.label;
61-
if (map.containsKey(keyN)) {
62-
map.put(keyN, map.get(keyN) + 1);
63-
} else {
64-
map.put(keyN, 1);
65-
}
66-
}
77+
for (DirectedGraphNode neighbor : node.neighbors) {
78+
int keyN = neighbor.label;
79+
if (map.containsKey(keyN)) {
80+
map.put(keyN, map.get(keyN) + 1);
81+
} else {
82+
map.put(keyN, 1);
83+
}
84+
}
6785
}
6886
//BFS: Add root node. Note:
6987
Queue<DirectedGraphNode> queue = new LinkedList<DirectedGraphNode>();
7088
for (DirectedGraphNode node : graph) {
71-
if (!map.containsKey(node.label)) {
72-
queue.offer(node);
73-
rst.add(node);
74-
}
89+
if (!map.containsKey(node.label)) {
90+
queue.offer(node);
91+
rst.add(node);
92+
}
7593
}
7694
//BFS: go through all children
7795
while (!queue.isEmpty()) {
78-
DirectedGraphNode node = queue.poll();
79-
for (DirectedGraphNode n : node.neighbors) {
80-
int label = n.label;
81-
map.put(label, map.get(label) - 1);
82-
if (map.get(label) == 0) {
83-
rst.add(n);
84-
queue.offer(n);
85-
}
86-
}
96+
DirectedGraphNode node = queue.poll();
97+
for (DirectedGraphNode n : node.neighbors) {
98+
int label = n.label;
99+
map.put(label, map.get(label) - 1);
100+
if (map.get(label) == 0) {
101+
rst.add(n);
102+
queue.offer(n);
103+
}
104+
}
87105
}
88106
return rst;
89107
}
90108
}
109+
110+
```

0 commit comments

Comments
 (0)