diff --git a/src/main/java/g0201_0300/s0207_course_schedule/Solution.java b/src/main/java/g0201_0300/s0207_course_schedule/Solution.java new file mode 100644 index 000000000..b217c9b24 --- /dev/null +++ b/src/main/java/g0201_0300/s0207_course_schedule/Solution.java @@ -0,0 +1,41 @@ +package g0201_0300.s0207_course_schedule; + +import java.util.ArrayList; + +@SuppressWarnings("unchecked") +public class Solution { + private static final int WHITE = 0; + private static final int GRAY = 1; + private static final int BLACK = 2; + + public boolean canFinish(int numCourses, int[][] prerequisites) { + ArrayList[] adj = new ArrayList[numCourses]; + for (int i = 0; i < numCourses; i++) { + adj[i] = new ArrayList<>(); + } + for (int[] pre : prerequisites) { + adj[pre[1]].add(pre[0]); + } + int[] colors = new int[numCourses]; + for (int i = 0; i < numCourses; i++) { + if (colors[i] == WHITE && !adj[i].isEmpty() && hasCycle(adj, i, colors)) { + return false; + } + } + return true; + } + + private boolean hasCycle(ArrayList[] adj, int node, int[] colors) { + colors[node] = GRAY; + for (int nei : adj[node]) { + if (colors[nei] == GRAY) { + return true; + } + if (colors[nei] == WHITE && hasCycle(adj, nei, colors)) { + return true; + } + } + colors[node] = BLACK; + return false; + } +} diff --git a/src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/Trie.java b/src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/Trie.java new file mode 100644 index 000000000..5d1e18404 --- /dev/null +++ b/src/main/java/g0201_0300/s0208_implement_trie_prefix_tree/Trie.java @@ -0,0 +1,64 @@ +package g0201_0300.s0208_implement_trie_prefix_tree; + +@SuppressWarnings("java:S1104") +public class Trie { + static class TrieNode { + // Initialize your data structure here. + public TrieNode[] children; + public boolean isWord; + + public TrieNode() { + children = new TrieNode[26]; + } + } + + private TrieNode root; + private boolean startWith; + + public Trie() { + root = new TrieNode(); + } + + // Inserts a word into the trie. + public void insert(String word) { + insert(word, root, 0); + } + + private void insert(String word, TrieNode root, int idx) { + if (idx == word.length()) { + root.isWord = true; + return; + } + int index = word.charAt(idx) - 'a'; + if (root.children[index] == null) { + root.children[index] = new TrieNode(); + } + insert(word, root.children[index], idx + 1); + } + + // Returns if the word is in the trie. + public boolean search(String word) { + return search(word, root, 0); + } + + public boolean search(String word, TrieNode root, int idx) { + if (idx == word.length()) { + startWith = true; + return root.isWord; + } + int index = word.charAt(idx) - 'a'; + if (root.children[index] == null) { + startWith = false; + return false; + } + + return search(word, root.children[index], idx + 1); + } + + // Returns if there is any word in the trie + // that starts with the given prefix. + public boolean startsWith(String prefix) { + search(prefix); + return startWith; + } +} diff --git a/src/main/java/g0201_0300/s0209_minimum_size_subarray_sum/Solution.java b/src/main/java/g0201_0300/s0209_minimum_size_subarray_sum/Solution.java new file mode 100644 index 000000000..49aa30392 --- /dev/null +++ b/src/main/java/g0201_0300/s0209_minimum_size_subarray_sum/Solution.java @@ -0,0 +1,33 @@ +package g0201_0300.s0209_minimum_size_subarray_sum; + +public class Solution { + public int minSubArrayLen(int target, int[] nums) { + int i = 0; + int j = 0; + int sum = 0; + int min = Integer.MAX_VALUE; + while (j < nums.length) { + sum += nums[j]; + if (sum < target) { + j++; + } else { + while (i <= j) { + if (sum - nums[i] >= target) { + sum = sum - nums[i]; + i++; + } else { + break; + } + } + if (j - i + 1 < min) { + min = j - i + 1; + } + j++; + } + } + if (min == Integer.MAX_VALUE) { + return 0; + } + return min; + } +} diff --git a/src/test/java/g0201_0300/s0207_course_schedule/SolutionTest.java b/src/test/java/g0201_0300/s0207_course_schedule/SolutionTest.java new file mode 100644 index 000000000..9d4f62e8f --- /dev/null +++ b/src/test/java/g0201_0300/s0207_course_schedule/SolutionTest.java @@ -0,0 +1,18 @@ +package g0201_0300.s0207_course_schedule; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; + +public class SolutionTest { + @Test + public void canFinish() { + assertThat(new Solution().canFinish(2, new int[][] {{1, 0}}), equalTo(true)); + } + + @Test + public void canFinish2() { + assertThat(new Solution().canFinish(2, new int[][] {{1, 0}, {0, 1}}), equalTo(false)); + } +} diff --git a/src/test/java/g0201_0300/s0208_implement_trie_prefix_tree/TrieTest.java b/src/test/java/g0201_0300/s0208_implement_trie_prefix_tree/TrieTest.java new file mode 100644 index 000000000..d8ab47973 --- /dev/null +++ b/src/test/java/g0201_0300/s0208_implement_trie_prefix_tree/TrieTest.java @@ -0,0 +1,23 @@ +package g0201_0300.s0208_implement_trie_prefix_tree; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; + +public class TrieTest { + @Test + public void trie() { + Trie trie = new Trie(); + trie.insert("apple"); + // return True + assertThat(trie.search("apple"), equalTo(true)); + // return False + assertThat(trie.search("app"), equalTo(false)); + // return True + assertThat(trie.startsWith("app"), equalTo(true)); + trie.insert("app"); + // return True + assertThat(trie.search("app"), equalTo(true)); + } +} diff --git a/src/test/java/g0201_0300/s0209_minimum_size_subarray_sum/SolutionTest.java b/src/test/java/g0201_0300/s0209_minimum_size_subarray_sum/SolutionTest.java new file mode 100644 index 000000000..d09927de7 --- /dev/null +++ b/src/test/java/g0201_0300/s0209_minimum_size_subarray_sum/SolutionTest.java @@ -0,0 +1,24 @@ +package g0201_0300.s0209_minimum_size_subarray_sum; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.Test; + +public class SolutionTest { + @Test + public void minSubArrayLen() { + assertThat(new Solution().minSubArrayLen(7, new int[] {2, 3, 1, 2, 4, 3}), equalTo(2)); + } + + @Test + public void minSubArrayLen2() { + assertThat(new Solution().minSubArrayLen(4, new int[] {1, 4, 4}), equalTo(1)); + } + + @Test + public void minSubArrayLen3() { + assertThat( + new Solution().minSubArrayLen(11, new int[] {1, 1, 1, 1, 1, 1, 1, 1}), equalTo(0)); + } +}