Skip to content

Commit

Permalink
update 207 java, py, cheatsheet, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Jun 10, 2024
1 parent 3e2cef9 commit bfb838b
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 21 deletions.
4 changes: 2 additions & 2 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
20240610: 22(ok*)
20240609: 236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)
20240610: 22(ok*),207(todo),210(todo)
20240609: 236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)
20240608: 503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77
20240607: 235(ok),236(again),496(ok),110(ok),1448(again!)
20240606: 206,143,138(again),141,needcode_tree,226,104,110(again!!!)
Expand Down
32 changes: 16 additions & 16 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
2024-08-04 -> ['22(ok*)']
2024-08-03 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)']
2024-08-04 -> ['22(ok*),207(todo),210(todo)']
2024-08-03 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)']
2024-08-02 -> ['503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77']
2024-08-01 -> ['235(ok),236(again),496(ok),110(ok),1448(again!)']
2024-07-31 -> ['206,143,138(again),141,needcode_tree,226,104,110(again!!!)']
Expand All @@ -18,8 +18,8 @@
2024-07-17 -> ['56']
2024-07-16 -> ['62,572,57']
2024-07-15 -> ['322,191,190']
2024-07-14 -> ['22(ok*)', '73,200,70,323(again)']
2024-07-13 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '207,79,206,213,198']
2024-07-14 -> ['22(ok*),207(todo),210(todo)', '73,200,70,323(again)']
2024-07-13 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '207,79,206,213,198']
2024-07-12 -> ['503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '212(todo),211,338,208(again)']
2024-07-11 -> ['235(ok),236(again),496(ok),110(ok),1448(again!)', '347,253(todo),91(todo),217']
2024-07-10 -> ['206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '226,98,253(todo)']
Expand All @@ -30,28 +30,28 @@
2024-07-05 -> ['128,261', '121,252']
2024-07-04 -> ['005,003(again)', '125']
2024-07-02 -> ['11,647,261(again),133(again)']
2024-07-01 -> ['22(ok*)', '143,15,268,141,139']
2024-06-30 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '21,20,19,271,269']
2024-07-01 -> ['22(ok*),207(todo),210(todo)', '143,15,268,141,139']
2024-06-30 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '21,20,19,271,269']
2024-06-29 -> ['503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '417(again),33,153,152']
2024-06-28 -> ['235(ok),236(again),496(ok),110(ok),1448(again!)', '424,297(todo),295(todo),39']
2024-06-27 -> ['206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '55,54(todo),53(again),435(again),49,48(todo),300(again)']
2024-06-26 -> ['150,22(again),739(again!!!),needcode_linkedlist', '56']
2024-06-25 -> ['567(todo),needcode_array_hash,needcode_sliding_window,567(again),needcode_stack,155(again)', '62,572,57']
2024-06-24 -> ['146,460,582(ok*),139(ok),322(todo)', '322,191,190']
2024-06-23 -> ['22(ok*)', '127,742(again),133(again),207(ok*),261(ok)', '73,200,70,323(again)']
2024-06-22 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '128,261', '207,79,206,213,198']
2024-06-23 -> ['22(ok*),207(todo),210(todo)', '127,742(again),133(again),207(ok*),261(ok)', '73,200,70,323(again)']
2024-06-22 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '128,261', '207,79,206,213,198']
2024-06-21 -> ['503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '005,003(again)', '212(todo),211,338,208(again)']
2024-06-20 -> ['235(ok),236(again),496(ok),110(ok),1448(again!)', '347,253(todo),91(todo),217']
2024-06-19 -> ['206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '11,647,261(again),133(again)', '226,98,253(todo)']
2024-06-18 -> ['22(ok*)', '150,22(again),739(again!!!),needcode_linkedlist', '143,15,268,141,139', '104,230,102,100']
2024-06-17 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '567(todo),needcode_array_hash,needcode_sliding_window,567(again),needcode_stack,155(again)', '21,20,19,271,269', '105,106']
2024-06-18 -> ['22(ok*),207(todo),210(todo)', '150,22(again),739(again!!!),needcode_linkedlist', '143,15,268,141,139', '104,230,102,100']
2024-06-17 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '567(todo),needcode_array_hash,needcode_sliding_window,567(again),needcode_stack,155(again)', '21,20,19,271,269', '105,106']
2024-06-16 -> ['503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '146,460,582(ok*),139(ok),322(todo)', '417(again),33,153,152', '242,235']
2024-06-15 -> ['22(ok*)', '235(ok),236(again),496(ok),110(ok),1448(again!)', '127,742(again),133(again),207(ok*),261(ok)', '424,297(todo),295(todo),39', '371']
2024-06-14 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '128,261', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '121,252']
2024-06-13 -> ['22(ok*)', '503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '150,22(again),739(again!!!),needcode_linkedlist', '005,003(again)', '56', '125']
2024-06-12 -> ['22(ok*)', '236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '235(ok),236(again),496(ok),110(ok),1448(again!)', '567(todo),needcode_array_hash,needcode_sliding_window,567(again),needcode_stack,155(again)', '62,572,57']
2024-06-11 -> ['22(ok*)', '236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '146,460,582(ok*),139(ok),322(todo)', '11,647,261(again),133(again)', '322,191,190']
2024-06-10 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),207(todo),261(todo)', '503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '235(ok),236(again),496(ok),110(ok),1448(again!)', '150,22(again),739(again!!!),needcode_linkedlist', '127,742(again),133(again),207(ok*),261(ok)', '143,15,268,141,139', '73,200,70,323(again)']
2024-06-15 -> ['22(ok*),207(todo),210(todo)', '235(ok),236(again),496(ok),110(ok),1448(again!)', '127,742(again),133(again),207(ok*),261(ok)', '424,297(todo),295(todo),39', '371']
2024-06-14 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '128,261', '55,54(todo),53(again),435(again),49,48(todo),300(again)', '121,252']
2024-06-13 -> ['22(ok*),207(todo),210(todo)', '503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '150,22(again),739(again!!!),needcode_linkedlist', '005,003(again)', '56', '125']
2024-06-12 -> ['22(ok*),207(todo),210(todo)', '236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '235(ok),236(again),496(ok),110(ok),1448(again!)', '567(todo),needcode_array_hash,needcode_sliding_window,567(again),needcode_stack,155(again)', '62,572,57']
2024-06-11 -> ['22(ok*),207(todo),210(todo)', '236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '146,460,582(ok*),139(ok),322(todo)', '11,647,261(again),133(again)', '322,191,190']
2024-06-10 -> ['236(again*),1448(again),needcode_trie,208(ok*),needcode_graph,695,22(todo),981(todo),261(todo)', '503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '235(ok),236(again),496(ok),110(ok),1448(again!)', '150,22(again),739(again!!!),needcode_linkedlist', '127,742(again),133(again),207(ok*),261(ok)', '143,15,268,141,139', '73,200,70,323(again)']
2024-06-09 -> ['503(todo),739(ok),105(todo),needcode_heap_pq,1046,215,needcode_backtrack,78,39(todo),46,90(todo),77', '235(ok),236(again),496(ok),110(ok),1448(again!)', '206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '567(todo),needcode_array_hash,needcode_sliding_window,567(again),needcode_stack,155(again)', '128,261', '21,20,19,271,269', '207,79,206,213,198']
2024-06-08 -> ['235(ok),236(again),496(ok),110(ok),1448(again!)', '206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '150,22(again),739(again!!!),needcode_linkedlist', '146,460,582(ok*),139(ok),322(todo)', '005,003(again)', '417(again),33,153,152', '212(todo),211,338,208(again)']
2024-06-07 -> ['206,143,138(again),141,needcode_tree,226,104,110(again!!!)', '150,22(again),739(again!!!),needcode_linkedlist', '567(todo),needcode_array_hash,needcode_sliding_window,567(again),needcode_stack,155(again)', '127,742(again),133(again),207(ok*),261(ok)', '424,297(todo),295(todo),39', '347,253(todo),91(todo),217']
Expand Down
101 changes: 101 additions & 0 deletions doc/cheatsheet/backtrack.md
Original file line number Diff line number Diff line change
Expand Up @@ -1432,4 +1432,105 @@ class Solution:
for parent in parents[r[0]]:
stack.append([parent]+r)
return res
```

### 2-11) Course Schedule
```java
// java
// LC 207
// V0
// IDEA : DFS (fix by gpt)
// NOTE !!! instead of maintain status (0,1,2), below video offers a simpler approach
// -> e.g. use a set, recording the current visiting course, if ANY duplicated (already in set) course being met,
// -> means "cyclic", so return false directly
// https://www.youtube.com/watch?v=EgI5nU9etnU
public boolean canFinish(int numCourses, int[][] prerequisites) {
// Initialize adjacency list for storing prerequisites
/**
* NOTE !!!
*
* init prerequisites map
* {course : [prerequisites_array]}
* below init map with null array as first step
*/
Map<Integer, List<Integer>> preMap = new HashMap<>();
for (int i = 0; i < numCourses; i++) {
preMap.put(i, new ArrayList<>());
}

// Populate the adjacency list with prerequisites
/**
* NOTE !!!
*
* update prerequisites map
* {course : [prerequisites_array]}
* so we go through prerequisites,
* then append each course's prerequisites to preMap
*/
for (int[] pair : prerequisites) {
int crs = pair[0];
int pre = pair[1];
preMap.get(crs).add(pre);
}

/** NOTE !!!
*
* init below set for checking if there is "cyclic" case
*/
// Set for tracking courses during the current DFS path
Set<Integer> visiting = new HashSet<>();

// Recursive DFS function
for (int c = 0; c < numCourses; c++) {
if (!dfs(c, preMap, visiting)) {
return false;
}
}
return true;
}

private boolean dfs(int crs, Map<Integer, List<Integer>> preMap, Set<Integer> visiting) {
/** NOTE !!!
*
* if visiting contains current course,
* means there is a "cyclic",
* (e.g. : needs to take course a, then can take course b, and needs to take course b, then can take course a)
* so return false directly
*/
if (visiting.contains(crs)) {
return false;
}
/**
* NOTE !!!
*
* if such course has NO preRequisite,
* return true directly
*/
if (preMap.get(crs).isEmpty()) {
return true;
}

/**
* NOTE !!!
*
* add current course to set (Set<Integer> visiting)
*/
visiting.add(crs);
for (int pre : preMap.get(crs)) {
if (!dfs(pre, preMap, visiting)) {
return false;
}
}
/**
* NOTE !!!
*
* remove current course from set,
* since already finish visiting
*
* e.g. undo changes
*/
visiting.remove(crs);
preMap.get(crs).clear(); // Clear prerequisites as the course is confirmed to be processed
return true;
}
```
2 changes: 1 addition & 1 deletion doc/cheatsheet/bfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class Solution(object):
# 207 Couese Schedule
# 210 Course Schedule II

# note : there is also a dfs solution
# note : there is also a dfs / backtrack solution
# IDEA : BFS
class Solution(object):
def canFinish(self, N, prerequisites):
Expand Down
92 changes: 90 additions & 2 deletions leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,100 @@
public class CourseSchedule {

// V0
// IDEA : DFS
// TODO : implement
// IDEA : DFS (fix by gpt)
// NOTE !!! instead of maintain status (0,1,2), below video offers a simpler approach
// -> e.g. use a set, recording the current visiting course, if ANY duplicated (already in set) course being met,
// -> means "cyclic", so return false directly
// https://www.youtube.com/watch?v=EgI5nU9etnU
public boolean canFinish(int numCourses, int[][] prerequisites) {
// Initialize adjacency list for storing prerequisites
/**
* NOTE !!!
*
* init prerequisites map
* {course : [prerequisites_array]}
* below init map with null array as first step
*/
Map<Integer, List<Integer>> preMap = new HashMap<>();
for (int i = 0; i < numCourses; i++) {
preMap.put(i, new ArrayList<>());
}

// Populate the adjacency list with prerequisites
/**
* NOTE !!!
*
* update prerequisites map
* {course : [prerequisites_array]}
* so we go through prerequisites,
* then append each course's prerequisites to preMap
*/
for (int[] pair : prerequisites) {
int crs = pair[0];
int pre = pair[1];
preMap.get(crs).add(pre);
}

/** NOTE !!!
*
* init below set for checking if there is "cyclic" case
*/
// Set for tracking courses during the current DFS path
Set<Integer> visiting = new HashSet<>();

// Recursive DFS function
for (int c = 0; c < numCourses; c++) {
if (!dfs(c, preMap, visiting)) {
return false;
}
}
return true;
}

private boolean dfs(int crs, Map<Integer, List<Integer>> preMap, Set<Integer> visiting) {
/** NOTE !!!
*
* if visiting contains current course,
* means there is a "cyclic",
* (e.g. : needs to take course a, then can take course b, and needs to take course b, then can take course a)
* so return false directly
*/
if (visiting.contains(crs)) {
return false;
}
/**
* NOTE !!!
*
* if such course has NO preRequisite,
* return true directly
*/
if (preMap.get(crs).isEmpty()) {
return true;
}

/**
* NOTE !!!
*
* add current course to set (Set<Integer> visiting)
*/
visiting.add(crs);
for (int pre : preMap.get(crs)) {
if (!dfs(pre, preMap, visiting)) {
return false;
}
}
/**
* NOTE !!!
*
* remove current course from set,
* since already finish visiting
*
* e.g. undo changes
*/
visiting.remove(crs);
preMap.get(crs).clear(); // Clear prerequisites as the course is confirmed to be processed
return true;
}

// VO'
// IDEA : TOPOLOGICAL SORT
Expand Down
105 changes: 105 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -4890,6 +4890,111 @@ public boolean isValidP(String input){
return true;
}

// LC 207
public boolean canFinish_5(int numCourses, int[][] prerequisites) {

if (prerequisites.length == 0){
return true;
}

if (prerequisites.length == 1 && numCourses == 1){
return true;
}

// init
// map : {course, [course_prerequisites]}
Map<Integer, List<Integer>> map = new HashMap<>();
for (int[] item : prerequisites){
if (!map.containsKey(item[0])){
map.put(item[0], new ArrayList<>());
}else{
List<Integer> cur = map.get(item[0]);
cur.add(item[1]);
map.put(item[0], cur);
}
}
// set : current visiting courses
HashSet<Integer> set = new HashSet<>();
HashSet<Integer> visited = new HashSet<>();

// dfs
int cur = 0; // ?
if (!checkPrereq(cur, map, set, visited)){
return false;
}
return set.size() == numCourses;
}

public boolean checkPrereq(int cur, Map<Integer, List<Integer>> map, HashSet<Integer> set, HashSet<Integer> visited){

visited.add(cur);

if (set.contains(cur)){
return false;
}

set.add(cur);
if (!map.containsKey(cur) || map.get(cur).size() == 0){
return true;
}

for (Integer x : map.get(cur)){
this.checkPrereq(x, map, set, visited);
}

// undo
set.remove(cur);

return true;
}

// LC 207
//import java.util.*;
public boolean canFinish_6(int numCourses, int[][] prerequisites) {
// Initialize adjacency list for storing prerequisites
Map<Integer, List<Integer>> preMap = new HashMap<>();
for (int i = 0; i < numCourses; i++) {
preMap.put(i, new ArrayList<>());
}

// Populate the adjacency list with prerequisites
for (int[] pair : prerequisites) {
int crs = pair[0];
int pre = pair[1];
preMap.get(crs).add(pre);
}

// Set for tracking courses during the current DFS path
Set<Integer> visiting = new HashSet<>();

// Recursive DFS function
for (int c = 0; c < numCourses; c++) {
if (!dfs(c, preMap, visiting)) {
return false;
}
}
return true;
}

private boolean dfs(int crs, Map<Integer, List<Integer>> preMap, Set<Integer> visiting) {
if (visiting.contains(crs)) {
return false;
}
if (preMap.get(crs).isEmpty()) {
return true;
}

visiting.add(crs);
for (int pre : preMap.get(crs)) {
if (!dfs(pre, preMap, visiting)) {
return false;
}
}
visiting.remove(crs);
preMap.get(crs).clear(); // Clear prerequisites as the course is confirmed to be processed
return true;
}



}
Loading

0 comments on commit bfb838b

Please sign in to comment.