Skip to content

Commit 845cdbd

Browse files
authored
[jinho] week5 (#878)
1 parent 767c2f3 commit 845cdbd

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestMaxProfit(t *testing.T) {
9+
prices := []int{7, 1, 5, 3, 6, 4}
10+
if maxProfit(prices) != 5 {
11+
t.Error("Test case 0 failed")
12+
}
13+
14+
prices = []int{7, 6, 4, 3, 1}
15+
if maxProfit(prices) != 0 {
16+
t.Error("Test case 1 failed")
17+
}
18+
}
19+
20+
func maxProfit(prices []int) int {
21+
minPrice := prices[0]
22+
result := 0
23+
24+
for _, price := range prices {
25+
profit := price - minPrice
26+
if profit > result {
27+
result = profit
28+
}
29+
if price < minPrice {
30+
minPrice = price
31+
}
32+
}
33+
return result
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import (
7+
"bytes"
8+
"fmt"
9+
"strconv"
10+
"testing"
11+
)
12+
13+
func TestEncodeAndDecode(t *testing.T) {
14+
strs := []string{"abc", "def", "ghi"}
15+
encoded := encode(strs)
16+
decoded := decode(encoded)
17+
if len(strs) != len(decoded) {
18+
t.Errorf("Expected %v but got %v", strs, decoded)
19+
}
20+
for i := 0; i < len(strs); i++ {
21+
if strs[i] != decoded[i] {
22+
t.Errorf("Expected %v but got %v", strs, decoded)
23+
}
24+
}
25+
}
26+
27+
func encode(strs []string) string {
28+
var buffer bytes.Buffer
29+
for _, str := range strs {
30+
buffer.WriteString(fmt.Sprintf("%d~", len(str)))
31+
buffer.WriteString(str)
32+
}
33+
return buffer.String()
34+
}
35+
36+
func decode(str string) []string {
37+
var result []string
38+
for i := 0; i < len(str); {
39+
j := i
40+
for str[j] != '~' {
41+
j++
42+
}
43+
length, _ := strconv.Atoi(str[i:j])
44+
result = append(result, str[j+1:j+1+length])
45+
i = j + 1 + length
46+
}
47+
return result
48+
}

group-anagrams/neverlish.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestGroupAnagrams(t *testing.T) {
9+
strs := []string{"eat", "tea", "tan", "ate", "nat", "bat"}
10+
result := groupAnagrams(strs)
11+
12+
if len(result) != 3 {
13+
t.Error("Test case 0 failed")
14+
}
15+
16+
if len(result[0]) != 3 {
17+
t.Error("Test case 1 failed")
18+
}
19+
20+
if len(result[1]) != 3 {
21+
t.Error("Test case 2 failed")
22+
}
23+
24+
if len(result[2]) != 1 {
25+
t.Error("Test case 3 failed")
26+
}
27+
}
28+
29+
func groupAnagrams(strs []string) [][]string {
30+
result := make([][]string, 0)
31+
32+
m := make(map[[26]int]int)
33+
34+
for _, str := range strs {
35+
key := [26]int{}
36+
for _, c := range str {
37+
key[c-'a']++
38+
}
39+
40+
if idx, ok := m[key]; ok {
41+
result[idx] = append(result[idx], str)
42+
} else {
43+
m[key] = len(result)
44+
result = append(result, []string{str})
45+
}
46+
}
47+
48+
return result
49+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
type Trie struct {
7+
children map[byte]*Trie
8+
isEnd bool
9+
}
10+
11+
func Constructor() Trie {
12+
return Trie{children: make(map[byte]*Trie)}
13+
}
14+
15+
func (this *Trie) Insert(word string) {
16+
for i := 0; i < len(word); i++ {
17+
if _, ok := this.children[word[i]]; !ok {
18+
this.children[word[i]] = &Trie{children: make(map[byte]*Trie)}
19+
}
20+
this = this.children[word[i]]
21+
}
22+
this.isEnd = true
23+
}
24+
25+
func (this *Trie) Search(word string) bool {
26+
for i := 0; i < len(word); i++ {
27+
if _, ok := this.children[word[i]]; !ok {
28+
return false
29+
}
30+
this = this.children[word[i]]
31+
}
32+
if this.isEnd {
33+
return true
34+
}
35+
return false
36+
}
37+
38+
func (this *Trie) StartsWith(prefix string) bool {
39+
for i := 0; i < len(prefix); i++ {
40+
if _, ok := this.children[prefix[i]]; !ok {
41+
return false
42+
}
43+
this = this.children[prefix[i]]
44+
}
45+
return true
46+
}
47+
48+
/**
49+
* Your Trie object will be instantiated and called as such:
50+
* obj := Constructor();
51+
* obj.Insert(word);
52+
* param_2 := obj.Search(word);
53+
* param_3 := obj.StartsWith(prefix);
54+
*/

word-break/neverlish.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// 시간복잡도: O(n^2)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestWordBreak(t *testing.T) {
9+
10+
result1 := wordBreak("leetcode", []string{"leet", "code"})
11+
if result1 != true {
12+
t.Error("Test case 0 failed")
13+
}
14+
15+
result2 := wordBreak("applepenapple", []string{"apple", "pen"})
16+
if result2 != true {
17+
t.Error("Test case 1 failed")
18+
}
19+
20+
result3 := wordBreak("catsandog", []string{"cats", "dog", "sand", "and", "cat"})
21+
if result3 != false {
22+
t.Error("Test case 2 failed")
23+
}
24+
}
25+
26+
func wordBreak(s string, wordDict []string) bool {
27+
wordMap := make(map[string]bool)
28+
29+
for _, word := range wordDict {
30+
wordMap[word] = true
31+
}
32+
33+
dp := make([]bool, len(s)+1)
34+
dp[0] = true
35+
for i := 1; i <= len(s); i++ {
36+
for j := 0; j < i; j++ {
37+
word := s[j:i]
38+
if dp[j] && wordMap[word] {
39+
dp[i] = true
40+
break
41+
}
42+
}
43+
}
44+
45+
return dp[len(s)]
46+
}

0 commit comments

Comments
 (0)