Skip to content

Commit 28170cf

Browse files
add 1462
1 parent 19234d9 commit 28170cf

File tree

3 files changed

+101
-2
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+101
-2
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
| 1469 | [Find All The Lonely Nodes](https://leetcode.com/problems/find-all-the-lonely-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1469.java) | | Easy | Tree, DFS |
235235
| 1466 | [Reorder Routes to Make All Paths Lead to the City Zero](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1466.java) | | Medium | Tree, DFS, BFS |
236236
| 1464 | [Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1464.java) | | Easy | Array |
237+
| 1462 | [Course Schedule IV](https://leetcode.com/problems/course-schedule-iv/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1462.java) | | Medium | Topological Sort, DFS, BFS |
237238
| 1461 | [Check If a String Contains All Binary Codes of Size K](https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1461.java) | | Medium | String, Bit Manipulation |
238239
| 1460 | [Make Two Arrays Equal by Reversing Sub-arrays](https://leetcode.com/problems/make-two-arrays-equal-by-reversing-sub-arrays/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1460.java) | | Easy | Array |
239240
| 1457 | [Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1457.java) | | Medium | Bit Manipulation, Tree, DFS |
@@ -398,7 +399,7 @@
398399
| 1143 | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1143.java) | | Medium | String, DP
399400
| 1138 | [Alphabet Board Path](https://leetcode.com/problems/alphabet-board-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1138.java) | [:tv:](https://youtu.be/rk-aB4rEOyU) | Medium | HashTable, String |
400401
| 1137 | [N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1137.java) | | Easy ||
401-
| 1136 | [Parallel Courses](https://leetcode.com/problems/parallel-courses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java) | | Medium |Topological Sort
402+
| 1136 | [Parallel Courses](https://leetcode.com/problems/parallel-courses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1136.java) | | Medium | Topological Sort
402403
| 1134 | [Armstrong Number](https://leetcode.com/problems/armstrong-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1134.java) | [:tv:](https://www.youtube.com/watch?v=HTL7fd4HPf4) | Easy ||
403404
| 1133 | [Largest Unique Number](https://leetcode.com/problems/largest-unique-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1133.java) | [:tv:](https://youtu.be/Fecpt1YZlCs) | Easy ||
404405
| 1128 | [Number of Equivalent Domino Pairs](https://leetcode.com/problems/number-of-equivalent-domino-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1128.java) | [:tv:](https://www.youtube.com/watch?v=7EpEEHAAxyw) | Easy ||
@@ -430,7 +431,7 @@
430431
| 1062 | [Longest Repeating Substring](https://leetcode.com/problems/longest-repeating-substring/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1062.java) | | Medium | String, Binary Search, DP, Rolling Hash, Suffix Array, Hash Function |
431432
| 1061 | [Lexicographically Smallest Equivalent String](https://leetcode.com/problems/lexicographically-smallest-equivalent-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1061.java) | [:tv:](https://youtu.be/HvCaMw58_94) | Medium | Union Find
432433
| 1060 | [Missing Element in Sorted Array](https://leetcode.com/problems/missing-element-in-sorted-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1060.java) | | Medium ||Binary Search
433-
| 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1059.java) | | Medium |DFS
434+
| 1059 | [All Paths from Source Lead to Destination](https://leetcode.com/problems/all-paths-from-source-lead-to-destination/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1059.java) | | Medium | DFS
434435
| 1057 | [Campus Bikes](https://leetcode.com/problems/campus-bikes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1057.java) | | Medium ||Greedy, Sort
435436
| 1056 | [Confusing Number](https://leetcode.com/problems/confusing-number/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1056.java) | | Easy ||
436437
| 1055 | [Fixed Point](https://leetcode.com/problems/fixed-point/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1055.java) | | Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class _1462 {
9+
public static class Solution1 {
10+
/**
11+
* My completely original solution.
12+
* DFS + part of topological sort (building the adjacency list)
13+
*/
14+
public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
15+
List<Integer>[] graph = new ArrayList[numCourses];
16+
for (int i = 0; i < numCourses; i++) {
17+
graph[i] = new ArrayList<>();
18+
}
19+
for (int[] pre : prerequisites) {
20+
graph[pre[0]].add(pre[1]);
21+
}
22+
List<Boolean> result = new ArrayList<>();
23+
//this cache is essential to speed things up, otherwise TLE on LeetCode
24+
Map<String, Boolean> cache = new HashMap<>();
25+
for (int[] query : queries) {
26+
result.add(isPrereq(query[0], query[1], graph, cache));
27+
}
28+
return result;
29+
}
30+
31+
private Boolean isPrereq(int pre, int target, List<Integer>[] graph, Map<String, Boolean> cache) {
32+
if (pre == target) {
33+
cache.put(pre + "-" + target, true);
34+
return true;
35+
}
36+
if (cache.containsKey(pre + "-" + target)) {
37+
return cache.get(pre + "-" + target);
38+
}
39+
for (int v : graph[pre]) {
40+
if (isPrereq(v, target, graph, cache)) {
41+
return true;
42+
}
43+
}
44+
cache.put(pre + "-" + target, false);
45+
return false;
46+
}
47+
}
48+
49+
public static class Solution2 {
50+
/**TODO: use BFS to solve this problem.*/
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.secondthousand;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions.secondthousand._1462;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class _1462Test {
13+
private static _1462.Solution1 solution1;
14+
15+
@BeforeEach
16+
public void setup() {
17+
solution1 = new _1462.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
assertEquals(Arrays.asList(false, true), solution1.checkIfPrerequisite(3,
23+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
24+
"[1,0],[2,0]"),
25+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray("[0,1],[2,0]")));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
assertEquals(Arrays.asList(true, true),
31+
solution1.checkIfPrerequisite(3,
32+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
33+
"[1,2],[1,0],[2,0]"),
34+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray("[1,0],[1,2]")));
35+
}
36+
37+
@Test
38+
public void test3() {
39+
assertEquals(Arrays.asList(true, false, true, true, true, true, true, true, false, false, true, true, false, false, true, true, true, true, false, false, true, false, true, false, true, false, true, true, false, true, true, false, false, true, false, false, true, true, true, false),
40+
solution1.checkIfPrerequisite(7,
41+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray(
42+
"[2,3],[2,1],[2,0],[3,4],[3,6],[5,1],[5,0],[1,4],[1,0],[4,0],[0,6]"),
43+
CommonUtils.convertLeetCodeRegularRectangleArrayInputIntoJavaArray("[3,0],[6,4],[5,6],[2,6],[2,3],[5,6],[4,0],[2,6],[3,5],[5,3],[1,6],[1,0],[3,5],[6,5],[2,3],[3,0],[3,4],[3,4],[2,5],[0,3],[4,0],[6,4],[5,0],[6,5],[5,6],[6,5],[1,0],[3,4],[1,5],[1,4],[3,6],[0,1],[1,2],[5,1],[5,3],[5,3],[3,4],[5,4],[5,4],[5,3]")));
44+
}
45+
46+
}

0 commit comments

Comments
 (0)