Skip to content

Commit cc4a722

Browse files
authored
Merge pull request #2497 from tedkimdev/tedkimdev/week5
[tedkimdev] WEEK 05 Solutions
2 parents f471c4a + b8168ed commit cc4a722

5 files changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
func maxProfit(prices []int) int {
4+
l, r := 0, 1
5+
max := 0
6+
7+
for r < len(prices) {
8+
if prices[l] < prices[r] {
9+
profit := prices[r] - prices[l]
10+
if profit > max {
11+
max = profit
12+
}
13+
} else {
14+
l = r
15+
}
16+
r++
17+
}
18+
19+
return max
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
type Solution struct{}
2+
3+
// TC: O(m)
4+
// SC: O(m + n)
5+
func (s *Solution) Encode(strs []string) string {
6+
encoded := ""
7+
for _, str := range strs {
8+
encoded += fmt.Sprintf("%d|%s", len(str), str)
9+
}
10+
11+
return encoded
12+
}
13+
14+
// TC: O(m)
15+
// SC: O(m + n)
16+
func (s *Solution) Decode(encoded string) []string {
17+
decoded := make([]string, 0)
18+
19+
i := 0
20+
for i < len(encoded) {
21+
j := i
22+
for encoded[j] != '|' {
23+
j++
24+
}
25+
length, _ := strconv.Atoi(encoded[i:j])
26+
i = j + 1
27+
decoded = append(decoded, encoded[i:i+length])
28+
i += length
29+
}
30+
31+
return decoded
32+
}

group-anagrams/tedkimdev.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// TC: O(m * n)
2+
// SC: O(m * n)
3+
func groupAnagrams(strs []string) [][]string {
4+
groups := map[[26]int][]string{}
5+
6+
for _, s := range strs {
7+
var count [26]int
8+
for _, c := range s {
9+
count[c-'a']++
10+
}
11+
groups[count] = append(groups[count], s)
12+
}
13+
14+
result := [][]string{}
15+
for _, group := range groups {
16+
result = append(result, group)
17+
}
18+
return result
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// TC: O(n)
2+
// SC: O(t) - Where n is the length of the string and t is the total number of TrieNodes created in the Trie.
3+
type PrefixTree struct {
4+
children map[rune]*PrefixTree
5+
isWord bool
6+
}
7+
8+
func Constructor() PrefixTree {
9+
return PrefixTree{
10+
children: map[rune]*PrefixTree{},
11+
isWord: false,
12+
}
13+
}
14+
15+
func (this *PrefixTree) Insert(word string) {
16+
cur := this
17+
for _, c := range word {
18+
if _, ok := cur.children[c]; !ok {
19+
child := Constructor()
20+
cur.children[c] = &child
21+
cur = cur.children[c]
22+
} else {
23+
cur = cur.children[c]
24+
}
25+
}
26+
cur.isWord = true
27+
}
28+
29+
func (this *PrefixTree) Search(word string) bool {
30+
cur := this
31+
for _, c := range word {
32+
if _, ok := cur.children[c]; ok {
33+
cur = cur.children[c]
34+
} else {
35+
return false
36+
}
37+
}
38+
return cur.isWord
39+
}
40+
41+
func (this *PrefixTree) StartsWith(prefix string) bool {
42+
cur := this
43+
for _, c := range prefix {
44+
if _, ok := cur.children[c]; ok {
45+
cur = cur.children[c]
46+
} else {
47+
return false
48+
}
49+
}
50+
51+
return cur != nil
52+
}

word-break/tedkimdev.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// TC: O((n * t) + (m * t))
2+
// SC: O(n + (m * t))
3+
func wordBreak(s string, wordDict []string) bool {
4+
trie := NewTrieNode()
5+
maxLength := 0
6+
for _, word := range wordDict {
7+
trie.Insert(word)
8+
if len(word) > maxLength {
9+
maxLength = len(word)
10+
}
11+
}
12+
13+
dp := make([]bool, len(s)+1)
14+
dp[len(s)] = true
15+
16+
for i := len(s) - 1; i >= 0; i-- {
17+
node := trie
18+
for j := i; j < len(s) && j < i+maxLength; j++ {
19+
c := s[j]
20+
if _, ok := node.children[rune(c)]; !ok {
21+
break
22+
}
23+
node = node.children[rune(c)]
24+
25+
if node.isWord && dp[j+1] {
26+
dp[i] = true
27+
break
28+
}
29+
}
30+
}
31+
32+
return dp[0]
33+
}
34+
35+
type TrieNode struct {
36+
children map[rune]*TrieNode
37+
isWord bool
38+
}
39+
40+
func NewTrieNode() *TrieNode {
41+
return &TrieNode{children: make(map[rune]*TrieNode)}
42+
}
43+
44+
func (t *TrieNode) Insert(word string) {
45+
node := t
46+
for _, char := range word {
47+
if _, ok := node.children[char]; !ok {
48+
node.children[char] = NewTrieNode()
49+
}
50+
node = node.children[char]
51+
}
52+
node.isWord = true
53+
}
54+
55+
// func (t *TrieNode) Search(s string, i, j int) bool {
56+
// node := t
57+
// for idx := i; idx <= j; idx++ {
58+
// char := rune(s[idx])
59+
// if _, ok := node.children[char]; !ok {
60+
// return false
61+
// }
62+
// node = node.children[char]
63+
// }
64+
// return node.isWord
65+
// }
66+
67+

0 commit comments

Comments
 (0)