diff --git a/data/progress.txt b/data/progress.txt index 67ac9a1a..1a9fc3a2 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20240519: 207 20240518: 213(todo),198(todo),212(todo),211,338,208(again) 20240517: 347,253(todo),91(todo),217 20240516: 226,98,253(todo) diff --git a/data/to_review.txt b/data/to_review.txt index e03588ea..54fa59b3 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,4 +1,4 @@ -2024-07-12 -> ['213(todo),198(todo),212(todo),211,338'] +2024-07-12 -> ['213(todo),198(todo),212(todo),211,338,208(again)'] 2024-07-11 -> ['347,253(todo),91(todo),217'] 2024-07-10 -> ['226,98,253(todo)'] 2024-07-09 -> ['104,230,102,100'] @@ -7,7 +7,7 @@ 2024-07-06 -> ['371'] 2024-07-05 -> ['121,252'] 2024-07-04 -> ['125'] -2024-06-21 -> ['213(todo),198(todo),212(todo),211,338'] +2024-06-21 -> ['213(todo),198(todo),212(todo),211,338,208(again)'] 2024-06-20 -> ['347,253(todo),91(todo),217'] 2024-06-19 -> ['226,98,253(todo)'] 2024-06-18 -> ['104,230,102,100'] @@ -16,7 +16,7 @@ 2024-06-15 -> ['371'] 2024-06-14 -> ['121,252'] 2024-06-13 -> ['125'] -2024-06-08 -> ['213(todo),198(todo),212(todo),211,338'] +2024-06-08 -> ['213(todo),198(todo),212(todo),211,338,208(again)'] 2024-06-07 -> ['347,253(todo),91(todo),217'] 2024-06-06 -> ['226,98,253(todo)'] 2024-06-05 -> ['104,230,102,100'] @@ -24,19 +24,19 @@ 2024-06-03 -> ['242,235'] 2024-06-02 -> ['371'] 2024-06-01 -> ['121,252'] -2024-05-31 -> ['213(todo),198(todo),212(todo),211,338', '125'] +2024-05-31 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '125'] 2024-05-30 -> ['347,253(todo),91(todo),217'] 2024-05-29 -> ['226,98,253(todo)'] 2024-05-28 -> ['104,230,102,100'] 2024-05-27 -> ['105,106'] -2024-05-26 -> ['213(todo),198(todo),212(todo),211,338', '242,235'] +2024-05-26 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '242,235'] 2024-05-25 -> ['347,253(todo),91(todo),217', '371'] 2024-05-24 -> ['226,98,253(todo)', '121,252'] -2024-05-23 -> ['213(todo),198(todo),212(todo),211,338', '104,230,102,100', '125'] +2024-05-23 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '104,230,102,100', '125'] 2024-05-22 -> ['347,253(todo),91(todo),217', '105,106'] -2024-05-21 -> ['213(todo),198(todo),212(todo),211,338', '226,98,253(todo)', '242,235'] -2024-05-20 -> ['213(todo),198(todo),212(todo),211,338', '347,253(todo),91(todo),217', '104,230,102,100', '371'] -2024-05-19 -> ['213(todo),198(todo),212(todo),211,338', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252'] +2024-05-21 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '226,98,253(todo)', '242,235'] +2024-05-20 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '104,230,102,100', '371'] +2024-05-19 -> ['213(todo),198(todo),212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '226,98,253(todo)', '105,106', '121,252'] 2024-05-18 -> ['347,253(todo),91(todo),217', '226,98,253(todo)', '104,230,102,100', '242,235', '125'] 2024-05-17 -> ['226,98,253(todo)', '104,230,102,100', '105,106', '371'] 2024-05-16 -> ['104,230,102,100', '105,106', '242,235', '121,252'] @@ -211,7 +211,7 @@ 2023-08-29 -> ['131,17', '40', '1448', '113,257'] 2023-08-28 -> ['131,17', '90', '199'] 2023-08-27 -> ['79', '46', '112'] -2023-08-26 -> ['40', '78,39', '572,235', '207'] +2023-08-26 -> ['207', '40', '78,39', '572,235'] 2023-08-25 -> ['79', '90', '355', '2,146', '404'] 2023-08-24 -> ['79', '40', '46', '621', '138'] 2023-08-23 -> ['79', '40', '90', '981,143'] @@ -231,7 +231,7 @@ 2023-08-08 -> ['1448', '452', '113,257'] 2023-08-07 -> ['33', '208,211', '199', '617'] 2023-08-06 -> ['230,105', '875,153', '112'] -2023-08-05 -> ['098', '572,235', '853,74', '207', '543,572'] +2023-08-05 -> ['207', '098', '572,235', '853,74', '543,572'] 2023-08-04 -> ['208,211', '2,146', '739,150,22', '404'] 2023-08-03 -> ['230,105', '1448', '138'] 2023-08-02 -> ['208,211', '098', '199', '981,143'] @@ -244,7 +244,7 @@ 2023-07-26 -> ['199', '572,235', '138', '452', '113,257'] 2023-07-25 -> ['33', '572,235', '2,146', '981,143'] 2023-07-24 -> ['572,235', '2,146', '138', '875,153', '567,155', '112'] -2023-07-23 -> ['2,146', '138', '981,143', '452', '853,74', '19', '207'] +2023-07-23 -> ['207', '2,146', '138', '981,143', '452', '853,74', '19'] 2023-07-22 -> ['33', '138', '981,143', '739,150,22', '128,167,3,424', '404', '687'] 2023-07-21 -> ['981,143', '452', '875,153', '36,271', '637,653', '1047'] 2023-07-20 -> ['33', '452', '853,74', '347'] @@ -252,18 +252,18 @@ 2023-07-18 -> ['33', '875,153', '853,74', '19', '113,257'] 2023-07-17 -> ['875,153', '853,74', '739,150,22', '128,167,3,424', '104,110', '617'] 2023-07-16 -> ['853,74', '739,150,22', '567,155', '36,271', '112'] -2023-07-15 -> ['739,150,22', '19', '347', '207', '162', '543,572'] +2023-07-15 -> ['207', '739,150,22', '19', '347', '162', '543,572'] 2023-07-14 -> ['567,155', '128,167,3,424', '404', '496'] 2023-07-13 -> ['567,155', '19', '36,271', '113,257'] 2023-07-12 -> ['567,155', '19', '128,167,3,424', '347'] 2023-07-11 -> ['19', '128,167,3,424', '36,271', '112'] -2023-07-10 -> ['128,167,3,424', '36,271', '347', '113,257', '207', '160,203'] +2023-07-10 -> ['207', '128,167,3,424', '36,271', '347', '113,257', '160,203'] 2023-07-09 -> ['36,271', '347', '404', '104,110', '687', '1353,21,24'] 2023-07-08 -> ['347', '113,257', '112', '637,653', '101'] -2023-07-07 -> ['113,257', '207', '162', '067'] +2023-07-07 -> ['207', '113,257', '162', '067'] 2023-07-06 -> ['113,257', '112', '404'] -2023-07-05 -> ['112', '207'] -2023-07-04 -> ['112', '207', '404', '104,110', '617'] +2023-07-05 -> ['207', '112'] +2023-07-04 -> ['207', '112', '404', '104,110', '617'] 2023-07-03 -> ['207', '404'] 2023-07-02 -> ['404', '162', '543,572', '038'] 2023-07-01 -> ['104,110', '687', '674'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java b/leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java index 3c571bae..44fb5bb0 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java +++ b/leetcode_java/src/main/java/LeetCodeJava/BFS/CourseSchedule.java @@ -167,6 +167,26 @@ public boolean canFinish_0_2(int numCourses, int[][] prerequisites) { } private boolean dfs(List res, Map> graph, int[] visited, int course) { + + /** NOTE !!! + * + * here we maintain 3 status: + * + * status = 0 : not visited + * status = 1 : visiting + * status = 2 : visited + * + * -> + * So, + * if status == 2, return true directly, since such course already been visited, we should not visit it again + * + * if status = 1, should return false directly, since "it is being visiting now", + * any other progress try to visit the same course at the same time + * means course conflict, -> can't take such course + * + * + * Ref : https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Breadth-First-Search/course-schedule.py#L44 + */ if (visited[course] == 1) { return false; } diff --git a/leetcode_java/src/main/java/dev/workspace3.java b/leetcode_java/src/main/java/dev/workspace3.java index c7428537..582702ee 100644 --- a/leetcode_java/src/main/java/dev/workspace3.java +++ b/leetcode_java/src/main/java/dev/workspace3.java @@ -802,4 +802,77 @@ public boolean startsWith(String prefix) { } } + // LC 207 + // dfs ? + // 0930 am + /** + * prerequisites[i] = [ai, bi] + * indicates that + * you must take course "bi" first + * if you want to take course "ai" + */ + public boolean canFinish(int numCourses, int[][] prerequisites) { + + if (numCourses == 0 && prerequisites.length == 0){ + return true; + } + + if (prerequisites.length == 0){ + return true; + } + + Map> map = new HashMap<>(); + + for (int[] x : prerequisites){ + if (!map.containsKey(x[0])){ + List cur = new ArrayList<>(); + cur.add(x[1]); + map.put(x[0], cur); + }else{ + List cur = map.get(x[0]); + cur.add(x[1]); + map.put(x[0], cur); + } + } + + System.out.println("map = " + map); + + int[] visited = new int[numCourses]; + for (int i = 0; i < numCourses; i++){ + visited[i] = 0; + } + + for (int i = 0; i < numCourses; i++){ + if (! this._help_check(i, visited, map)){ + return false; + } + } + + return true; + //return Arrays.stream(visited).sum() == numCourses; + } + + public boolean _help_check(int courseId, int[] visited, Map> map){ + + System.out.println("---> courseId = " + courseId + " map = " + map + " visited = " + visited); + + if (!map.containsKey(courseId)){ + return true; + } + + for (int val : map.get(courseId)){ + if (visited[courseId] == 1){ + return false; + } + if (courseId == val){ + return false; + } + visited[courseId] = 1; + this._help_check(val, visited, map); + visited[courseId] = 0; + } + + return true; + } + }