Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6e208bd

Browse files
authored
Added tasks 207-209.
1 parent bfd26d2 commit 6e208bd

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g0201_0300.s0207_course_schedule;
2+
3+
import java.util.ArrayList;
4+
5+
@SuppressWarnings("unchecked")
6+
public class Solution {
7+
private static final int WHITE = 0;
8+
private static final int GRAY = 1;
9+
private static final int BLACK = 2;
10+
11+
public boolean canFinish(int numCourses, int[][] prerequisites) {
12+
ArrayList<Integer>[] adj = new ArrayList[numCourses];
13+
for (int i = 0; i < numCourses; i++) {
14+
adj[i] = new ArrayList<>();
15+
}
16+
for (int[] pre : prerequisites) {
17+
adj[pre[1]].add(pre[0]);
18+
}
19+
int[] colors = new int[numCourses];
20+
for (int i = 0; i < numCourses; i++) {
21+
if (colors[i] == WHITE && !adj[i].isEmpty() && hasCycle(adj, i, colors)) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
private boolean hasCycle(ArrayList<Integer>[] adj, int node, int[] colors) {
29+
colors[node] = GRAY;
30+
for (int nei : adj[node]) {
31+
if (colors[nei] == GRAY) {
32+
return true;
33+
}
34+
if (colors[nei] == WHITE && hasCycle(adj, nei, colors)) {
35+
return true;
36+
}
37+
}
38+
colors[node] = BLACK;
39+
return false;
40+
}
41+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g0201_0300.s0208_implement_trie_prefix_tree;
2+
3+
@SuppressWarnings("java:S1104")
4+
public class Trie {
5+
static class TrieNode {
6+
// Initialize your data structure here.
7+
public TrieNode[] children;
8+
public boolean isWord;
9+
10+
public TrieNode() {
11+
children = new TrieNode[26];
12+
}
13+
}
14+
15+
private TrieNode root;
16+
private boolean startWith;
17+
18+
public Trie() {
19+
root = new TrieNode();
20+
}
21+
22+
// Inserts a word into the trie.
23+
public void insert(String word) {
24+
insert(word, root, 0);
25+
}
26+
27+
private void insert(String word, TrieNode root, int idx) {
28+
if (idx == word.length()) {
29+
root.isWord = true;
30+
return;
31+
}
32+
int index = word.charAt(idx) - 'a';
33+
if (root.children[index] == null) {
34+
root.children[index] = new TrieNode();
35+
}
36+
insert(word, root.children[index], idx + 1);
37+
}
38+
39+
// Returns if the word is in the trie.
40+
public boolean search(String word) {
41+
return search(word, root, 0);
42+
}
43+
44+
public boolean search(String word, TrieNode root, int idx) {
45+
if (idx == word.length()) {
46+
startWith = true;
47+
return root.isWord;
48+
}
49+
int index = word.charAt(idx) - 'a';
50+
if (root.children[index] == null) {
51+
startWith = false;
52+
return false;
53+
}
54+
55+
return search(word, root.children[index], idx + 1);
56+
}
57+
58+
// Returns if there is any word in the trie
59+
// that starts with the given prefix.
60+
public boolean startsWith(String prefix) {
61+
search(prefix);
62+
return startWith;
63+
}
64+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g0201_0300.s0209_minimum_size_subarray_sum;
2+
3+
public class Solution {
4+
public int minSubArrayLen(int target, int[] nums) {
5+
int i = 0;
6+
int j = 0;
7+
int sum = 0;
8+
int min = Integer.MAX_VALUE;
9+
while (j < nums.length) {
10+
sum += nums[j];
11+
if (sum < target) {
12+
j++;
13+
} else {
14+
while (i <= j) {
15+
if (sum - nums[i] >= target) {
16+
sum = sum - nums[i];
17+
i++;
18+
} else {
19+
break;
20+
}
21+
}
22+
if (j - i + 1 < min) {
23+
min = j - i + 1;
24+
}
25+
j++;
26+
}
27+
}
28+
if (min == Integer.MAX_VALUE) {
29+
return 0;
30+
}
31+
return min;
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0201_0300.s0207_course_schedule;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void canFinish() {
11+
assertThat(new Solution().canFinish(2, new int[][] {{1, 0}}), equalTo(true));
12+
}
13+
14+
@Test
15+
public void canFinish2() {
16+
assertThat(new Solution().canFinish(2, new int[][] {{1, 0}, {0, 1}}), equalTo(false));
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0201_0300.s0208_implement_trie_prefix_tree;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class TrieTest {
9+
@Test
10+
public void trie() {
11+
Trie trie = new Trie();
12+
trie.insert("apple");
13+
// return True
14+
assertThat(trie.search("apple"), equalTo(true));
15+
// return False
16+
assertThat(trie.search("app"), equalTo(false));
17+
// return True
18+
assertThat(trie.startsWith("app"), equalTo(true));
19+
trie.insert("app");
20+
// return True
21+
assertThat(trie.search("app"), equalTo(true));
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g0201_0300.s0209_minimum_size_subarray_sum;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void minSubArrayLen() {
11+
assertThat(new Solution().minSubArrayLen(7, new int[] {2, 3, 1, 2, 4, 3}), equalTo(2));
12+
}
13+
14+
@Test
15+
public void minSubArrayLen2() {
16+
assertThat(new Solution().minSubArrayLen(4, new int[] {1, 4, 4}), equalTo(1));
17+
}
18+
19+
@Test
20+
public void minSubArrayLen3() {
21+
assertThat(
22+
new Solution().minSubArrayLen(11, new int[] {1, 1, 1, 1, 1, 1, 1, 1}), equalTo(0));
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /