Skip to content

Commit f34b6fd

Browse files
committed
add 444 java, progress
1 parent 1945512 commit f34b6fd

File tree

6 files changed

+273
-17
lines changed

6 files changed

+273
-17
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- [LC top 100 likes](https://leetcode.com/studyplan/top-interview-150/)
3030
- [neetcode 150 LC list](https://neetcode.io/practice)
3131
- [My Brain after 569 Leetcode Problems](https://youtu.be/8wysIxzqgPI)
32+
- [jiakaobo LC](https://www.jiakaobo.com/leetcode.html) : LC code & video
3233
- [LC pattern @ blind](https://www.teamblind.com/post/New-Year-Gift---Curated-List-of-Top-100-LeetCode-Questions-to-Save-Your-Time-OaM1orEU) : Curated-List-of-Top-100-LeetCode-Questions-to-Save-Your-Time
3334
- [LC Algorithm Problem Classification](https://www.programcreek.com/2013/08/leetcode-problem-classification/)
3435
- [cheatsheet-leetcode-a4](https://cheatsheet.dennyzhang.com/cheatsheet-leetcode-a4)
@@ -922,7 +923,8 @@
922923
286| [Walls and Gates](https://leetcode.com/problems/walls-and-gates/)| [Python](./leetcode_python/Breadth-First-Search/walls-and-gates.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/WallsAndGates.java)| _O(m * n)_ | _O(g)_ | Medium | 🔒, `dfs`, `bfs` , `good basic`, `google`,`amazon`,`FB`| AGAIN******** (7)
923924
310| [Minimum Height Trees](https://leetcode.com/problems/minimum-height-trees/)| [Python](./leetcode_python/Breadth-First-Search/minimum-height-trees.py) | _O(n)_ | _O(n)_ | Medium |`complex`| AGAIN (3) (not start)
924925
433| [Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/)| [Python](./leetcode_python/Breadth-First-Search/minimum-genetic-mutation.py) | _O(n * b)_ | _O(b)_ | Medium |check `# 127 Word Ladder`, `good basic`| AGAIN*** (3)
925-
444| [Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/)| [Python](./leetcode_python/Breadth-First-Search/sequence-reconstruction.py) | _O(n * s)_ | _O(n)_ | Medium | 🔒 Topological Sort , `google`, `airbnb`| AGAIN** (3) (not start)
926+
444| [Sequence Reconstruction](https://leetcode.com/problems/sequence-reconstruction/)| [Python](./leetcode_python/Breadth-First-Search/sequence-reconstruction.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BFS/SequenceReconstruction.java)
927+
| _O(n * s)_ | _O(n)_ | Medium | good trick, `Topological Sort` , `google`, `airbnb`| AGAIN******* (4) (not start)
926928
490| [The Maze](https://leetcode.com/problems/the-maze/)| [Python](./leetcode_python/Breadth-First-Search/the-maze.py) | _O(max(r, c) * w)_ | _O(w)_ | Medium | `basic`, `bfs`, `dfs`,`amazon`,`fb`| AGAIN**** (5)
927929
505| [The Maze II](https://leetcode.com/problems/the-maze-ii/)|[Python](./leetcode_python/Breadth-First-Search/the-maze-ii.py) | _O(max(r, c) * wlogw)_ | _O(w)_ | Medium | `trick`,`bfs`,`dfs`,`AGAIN`, `google`, `fb`| AGAIN********* (6)
928930
542| [01 Matrix](https://leetcode.com/problems/01-matrix/)| [Python](./leetcode_python/Breadth-First-Search/01-matrix.py) | _O(m * n)_ | _O(m * n)_ | Medium | DP, `trick`,`queue` | AGAIN*** (3)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-09
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-08
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
20241108: 1188,130,855(again),210(todo)
1+
20241109: 210(todo),444
2+
20241108: 1188,130,855(again)
23
20241104: 207
34
20241103: 900
45
20241102: 26,27

data/to_review.txt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
2025-01-02 -> ['1188,130,855(again),210(todo)']
1+
2025-01-03 -> ['210(todo),444']
2+
2025-01-02 -> ['1188,130,855(again)']
23
2024-12-28 -> ['900']
34
2024-12-27 -> ['26,27', '802,1197,26']
45
2024-12-21 -> ['855,846']
56
2024-12-20 -> ['932']
67
2024-12-18 -> ['951,792']
78
2024-12-14 -> ['163,1048']
8-
2024-12-13 -> ['298,729']
9-
2024-12-12 -> ['1188,130,855(again),210(todo)', '1146']
9+
2024-12-13 -> ['210(todo),444', '298,729']
10+
2024-12-12 -> ['1188,130,855(again)', '1146']
1011
2024-12-08 -> ['737']
1112
2024-12-07 -> ['900', '686,734,737']
1213
2024-12-06 -> ['26,27', '802,1197,26', '353']
1314
2024-12-05 -> ['528,334']
1415
2024-12-03 -> ['1145']
15-
2024-11-30 -> ['855,846', '1145,1219']
16-
2024-11-29 -> ['1188,130,855(again),210(todo)', '932']
16+
2024-11-30 -> ['210(todo),444', '855,846', '1145,1219']
17+
2024-11-29 -> ['1188,130,855(again)', '932']
1718
2024-11-27 -> ['951,792', '524,221,889']
1819
2024-11-26 -> ['743,889']
1920
2024-11-25 -> ['837']
2021
2024-11-24 -> ['900']
2122
2024-11-23 -> ['26,27', '802,1197,26', '163,1048', '981']
22-
2024-11-22 -> ['298,729', '1087']
23-
2024-11-21 -> ['1188,130,855(again),210(todo)', '1146']
23+
2024-11-22 -> ['210(todo),444', '298,729', '1087']
24+
2024-11-21 -> ['1188,130,855(again)', '1146']
2425
2024-11-20 -> ['939']
2526
2024-11-18 -> ['430']
26-
2024-11-17 -> ['855,846', '737', '363']
27-
2024-11-16 -> ['1188,130,855(again),210(todo)', '900', '932', '686,734,737', '1032,844,1011']
27+
2024-11-17 -> ['210(todo),444', '855,846', '737', '363']
28+
2024-11-16 -> ['1188,130,855(again)', '900', '932', '686,734,737', '1032,844,1011']
2829
2024-11-15 -> ['26,27', '802,1197,26', '353', '947']
29-
2024-11-14 -> ['951,792', '528,334']
30-
2024-11-13 -> ['1188,130,855(again),210(todo)']
31-
2024-11-12 -> ['1145', '753']
32-
2024-11-11 -> ['1188,130,855(again),210(todo)', '900', '727']
33-
2024-11-10 -> ['1188,130,855(again),210(todo)', '26,27', '802,1197,26', '163,1048']
34-
2024-11-09 -> ['1188,130,855(again),210(todo)', '855,846', '298,729', '1145,1219']
30+
2024-11-14 -> ['210(todo),444', '951,792', '528,334']
31+
2024-11-13 -> ['1188,130,855(again)']
32+
2024-11-12 -> ['210(todo),444', '1145', '753']
33+
2024-11-11 -> ['210(todo),444', '1188,130,855(again)', '900', '727']
34+
2024-11-10 -> ['210(todo),444', '1188,130,855(again)', '26,27', '802,1197,26', '163,1048']
35+
2024-11-09 -> ['1188,130,855(again)', '855,846', '298,729', '1145,1219']
3536
2024-11-08 -> ['900', '932', '1146']
3637
2024-11-07 -> ['26,27', '802,1197,26']
3738
2024-11-06 -> ['900', '951,792', '524,221,889']
Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
package LeetCodeJava.BFS;
2+
3+
// https://leetcode.com/problems/sequence-reconstruction/description/
4+
// https://leetcode.ca/all/444.html
5+
6+
import java.util.*;
7+
8+
/**
9+
* 444. Sequence Reconstruction
10+
* Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The org sequence is a permutation of the integers from 1 to n, with 1 ≤ n ≤ 104. Reconstruction means building a shortest common supersequence of the sequences in seqs (i.e., a shortest sequence so that all sequences in seqs are subsequences of it). Determine whether there is only one sequence that can be reconstructed from seqs and it is the org sequence.
11+
*
12+
* Example 1:
13+
*
14+
* Input:
15+
* org: [1,2,3], seqs: [[1,2],[1,3]]
16+
*
17+
* Output:
18+
* false
19+
*
20+
* Explanation:
21+
* [1,2,3] is not the only one sequence that can be reconstructed, because [1,3,2] is also a valid sequence that can be reconstructed.
22+
* Example 2:
23+
*
24+
* Input:
25+
* org: [1,2,3], seqs: [[1,2]]
26+
*
27+
* Output:
28+
* false
29+
*
30+
* Explanation:
31+
* The reconstructed sequence can only be [1,2].
32+
* Example 3:
33+
*
34+
* Input:
35+
* org: [1,2,3], seqs: [[1,2],[1,3],[2,3]]
36+
*
37+
* Output:
38+
* true
39+
*
40+
* Explanation:
41+
* The sequences [1,2], [1,3], and [2,3] can uniquely reconstruct the original sequence [1,2,3].
42+
* Example 4:
43+
*
44+
* Input:
45+
* org: [4,1,5,2,6,3], seqs: [[5,2,6,3],[4,1,5,2]]
46+
*
47+
* Output:
48+
* true
49+
* UPDATE (2017/1/8):
50+
* The seqs parameter had been changed to a list of list of strings (instead of a 2d array of strings). Please reload the code definition to get the latest changes.
51+
*
52+
* Difficulty:
53+
* Medium
54+
* Lock:
55+
* Prime
56+
* Company:
57+
* Google
58+
*
59+
*/
60+
public class SequenceReconstruction {
61+
62+
// V0
63+
// TODO : implement
64+
// public boolean sequenceReconstruction(int[] nums, List<List<Integer>> sequences) {
65+
//
66+
// }
67+
68+
// V1
69+
// https://www.youtube.com/watch?v=FHY1q1h9gq0
70+
// https://www.jiakaobo.com/leetcode/444.%20Sequence%20Reconstruction.html
71+
Map<Integer, Set<Integer>> map;
72+
Map<Integer, Integer> indegree;
73+
74+
public boolean sequenceReconstruction_1(int[] nums, List<List<Integer>> sequences) {
75+
map = new HashMap<>();
76+
indegree = new HashMap<>();
77+
78+
for(List<Integer> seq: sequences) {
79+
if(seq.size() == 1) {
80+
addNode(seq.get(0));
81+
} else {
82+
for(int i = 0; i < seq.size() - 1; i++) {
83+
addNode(seq.get(i));
84+
addNode(seq.get(i + 1));
85+
86+
// 加入子节点, 子节点增加一个入度
87+
// [1,2] => 1 -> 2
88+
// 1: [2]
89+
int curr = seq.get(i);
90+
int next = seq.get(i + 1);
91+
if(map.get(curr).add(next)) {
92+
indegree.put(next, indegree.get(next) + 1);
93+
}
94+
}
95+
}
96+
}
97+
98+
Queue<Integer> queue = new LinkedList<>();
99+
for(int key : indegree.keySet()) {
100+
if(indegree.get(key) == 0){
101+
queue.offer(key);
102+
}
103+
}
104+
105+
int index = 0;
106+
while(!queue.isEmpty()) {
107+
// 如果只有唯一解, 那么queue的大小永远都是1
108+
if(queue.size() != 1) return false;
109+
110+
int curr = queue.poll();
111+
if(curr != nums[index++]) return false;
112+
113+
for(int next: map.get(curr)) {
114+
indegree.put(next, indegree.get(next) - 1);
115+
if(indegree.get(next) == 0) {
116+
queue.offer(next);
117+
}
118+
}
119+
}
120+
121+
return index == nums.length;
122+
}
123+
124+
private void addNode(int node) {
125+
if(!map.containsKey(node)) {
126+
map.put(node, new HashSet<>());
127+
indegree.put(node, 0);
128+
}
129+
}
130+
131+
// V2
132+
// https://leetcode.ca/2017-02-16-444-Sequence-Reconstruction/
133+
public boolean sequenceReconstruction_2(int[] nums, List<List<Integer>> sequences) {
134+
int n = nums.length;
135+
int[] indeg = new int[n];
136+
List<Integer>[] g = new List[n];
137+
Arrays.setAll(g, k -> new ArrayList<>());
138+
for (List<Integer> seq : sequences) {
139+
for (int i = 1; i < seq.size(); ++i) {
140+
int a = seq.get(i - 1) - 1, b = seq.get(i) - 1;
141+
g[a].add(b);
142+
indeg[b]++;
143+
}
144+
}
145+
Deque<Integer> q = new ArrayDeque<>();
146+
for (int i = 0; i < n; ++i) {
147+
if (indeg[i] == 0) {
148+
q.offer(i);
149+
}
150+
}
151+
while (!q.isEmpty()) {
152+
if (q.size() > 1) {
153+
return false;
154+
}
155+
int i = q.poll();
156+
for (int j : g[i]) {
157+
if (--indeg[j] == 0) {
158+
q.offer(j);
159+
}
160+
}
161+
}
162+
return true;
163+
}
164+
165+
// V3
166+
// IDEA : topological sorting (gpt)
167+
// TODO : validate
168+
public boolean sequenceReconstruction_3(int[] org, List<List<Integer>> seqs) {
169+
int n = org.length;
170+
171+
// Step 1: Build the graph and calculate in-degrees
172+
Map<Integer, List<Integer>> graph = new HashMap<>();
173+
Map<Integer, Integer> inDegree = new HashMap<>();
174+
175+
for (int i = 1; i <= n; i++) {
176+
graph.put(i, new ArrayList<>());
177+
inDegree.put(i, 0);
178+
}
179+
180+
int count = 0; // Count valid nodes in seqs
181+
for (List<Integer> seq : seqs) {
182+
count += seq.size();
183+
for (int i = 0; i < seq.size(); i++) {
184+
if (seq.get(i) < 1 || seq.get(i) > n) {
185+
return false; // Invalid element in seqs
186+
}
187+
if (i > 0) {
188+
int prev = seq.get(i - 1), next = seq.get(i);
189+
graph.get(prev).add(next);
190+
inDegree.put(next, inDegree.get(next) + 1);
191+
}
192+
}
193+
}
194+
195+
// If seqs is empty or does not include enough information
196+
if (count < n) {
197+
return false;
198+
}
199+
200+
// Step 2: Topological Sort using BFS
201+
Queue<Integer> queue = new LinkedList<>();
202+
for (int key : inDegree.keySet()) {
203+
if (inDegree.get(key) == 0) {
204+
queue.offer(key);
205+
}
206+
}
207+
208+
int index = 0;
209+
while (!queue.isEmpty()) {
210+
if (queue.size() > 1) {
211+
return false; // More than one way to reconstruct
212+
}
213+
214+
int current = queue.poll();
215+
if (index == n || org[index] != current) {
216+
return false; // Current number does not match org
217+
}
218+
index++;
219+
220+
for (int neighbor : graph.get(current)) {
221+
inDegree.put(neighbor, inDegree.get(neighbor) - 1);
222+
if (inDegree.get(neighbor) == 0) {
223+
queue.offer(neighbor);
224+
}
225+
}
226+
}
227+
228+
// Check if we used all numbers in org
229+
return index == n;
230+
}
231+
232+
// V4
233+
// https://blog.csdn.net/qq_46105170/article/details/105727262
234+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,21 @@ private void dfsUpdate(int x, int y, char[][] board){
27052705
}
27062706
}
27072707

2708+
// LC 444
2709+
// https://leetcode.ca/all/444.html
2710+
// https://leetcode.ca/2017-02-16-444-Sequence-Reconstruction/
2711+
// 5.18 pm - 5.35 pm
2712+
/**
2713+
* step 1) build seq
2714+
* step 2) check if there is only 1 way ???
2715+
*
2716+
*
2717+
*/
2718+
public boolean sequenceReconstruction(int[] nums, List<List<Integer>> sequences) {
2719+
2720+
return false;
2721+
}
2722+
27082723

27092724
}
27102725

0 commit comments

Comments
 (0)