Skip to content

Added tasks 207-209. #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/main/java/g0201_0300/s0207_course_schedule/Solution.java
Original file line number Diff line number Diff line change
@@ -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<Integer>[] 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<Integer>[] 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/test/java/g0201_0300/s0207_course_schedule/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}