Skip to content

Commit f6bf5d4

Browse files
committed
2 problems
1 parent 4c1ca8d commit f6bf5d4

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
165165
#### [204. Count Primes](https://github.com/hitzzc/go-leetcode/tree/master/count_primes)
166166
#### [205. Isomorphic Strings](https://github.com/hitzzc/go-leetcode/tree/master/isomorphic_strings)
167167
#### [206. Reverse Linked List](https://github.com/hitzzc/go-leetcode/tree/master/reverse_linked_list)
168+
#### [207. Course Schedule](https://github.com/hitzzc/go-leetcode/tree/master/course_schedule)
169+
#### [208. Implement Trie (Prefix Tree)](https://github.com/hitzzc/go-leetcode/tree/master/implement_trie)
168170

169171

170172

course_schedule/course_schedule.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package course_schedule
2+
3+
// BFS
4+
func canFinish(numCourses int, prerequisites [][]int) bool {
5+
graph := map[int][]int{}
6+
inDegree := make([]int, numCourses)
7+
for i := range prerequisites {
8+
graph[prerequisites[i][1]] = append(graph[prerequisites[i][1]], prerequisites[i][0])
9+
inDegree[prerequisites[i][0]]++
10+
}
11+
queue := []int{}
12+
for i := range inDegree {
13+
if inDegree[i] == 0 {
14+
queue = append(queue, i)
15+
}
16+
}
17+
for len(queue) != 0 {
18+
head := queue[0]
19+
queue = queue[1:]
20+
for i := range graph[head] {
21+
inDegree[graph[head][i]]--
22+
if inDegree[graph[head][i]] == 0 {
23+
queue = append(queue, graph[head][i])
24+
}
25+
}
26+
}
27+
for i := range inDegree {
28+
if inDegree[i] != 0 {
29+
return false
30+
}
31+
}
32+
return true
33+
}
34+
35+
// DFS
36+
func canFinishDFS(numCourses int, prerequisites [][]int) bool {
37+
graph := map[int][]int{}
38+
for i := range prerequisites {
39+
graph[prerequisites[i][1]] = append(graph[prerequisites[i][1]], prerequisites[i][0])
40+
}
41+
path := make([]bool, numCourses)
42+
visited := make([]bool, numCourses)
43+
for i := 0; i < numCourses; i++ {
44+
if visited[i] {
45+
continue
46+
}
47+
if hasCycle(i, graph, path, visited) {
48+
return false
49+
}
50+
}
51+
return true
52+
}
53+
54+
func hasCycle(start int, graph map[int][]int, path []bool, visited []bool) bool {
55+
for i := range graph[start] {
56+
if visited[graph[start][i]] {
57+
continue
58+
}
59+
if path[graph[start][i]] {
60+
return true
61+
}
62+
path[graph[start][i]] = true
63+
if hasCycle(graph[start][i], graph, path, visited) {
64+
return true
65+
}
66+
path[graph[start][i]] = false
67+
}
68+
visited[start] = true
69+
return false
70+
}

implement_trie/implement_trie.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <unordered_map>
2+
using namespace std;
3+
4+
class TrieNode {
5+
public:
6+
TrieNode(): is_word_(false) {}
7+
public:
8+
bool is_word_;
9+
unordered_map<char, TrieNode*> children;
10+
};
11+
12+
class Trie {
13+
public:
14+
Trie() {
15+
root = new TrieNode();
16+
}
17+
18+
// Inserts a word into the trie.
19+
void insert(string word) {
20+
if (word.size() <= 0) return;
21+
TrieNode* node = root;
22+
for (int i = 0; i < word.size(); ++i){
23+
if (node->children.find(word[i]) == node->children.end()){
24+
node->children[word[i]] = new TrieNode();
25+
}
26+
node = node->children[word[i]];
27+
}
28+
node->is_word_ = true;
29+
return;
30+
}
31+
32+
// Returns if the word is in the trie.
33+
bool search(string word) {
34+
return retrieve(word, false);
35+
}
36+
37+
// Returns if there is any word in the trie
38+
// that starts with the given prefix.
39+
bool startsWith(string prefix) {
40+
return retrieve(prefix, true);
41+
}
42+
43+
private:
44+
TrieNode* root;
45+
private:
46+
bool retrieve(string& key, bool prefix){
47+
if (key.size() <= 0) return false;
48+
TrieNode* node = root;
49+
for (int i = 0; i < key.size(); i++){
50+
if (node->children.find(key[i]) == node->children.end())
51+
return false;
52+
node = node->children[key[i]];
53+
}
54+
return prefix ? true : node->is_word_;
55+
}
56+
};

0 commit comments

Comments
 (0)