diff --git a/best-time-to-buy-and-sell-stock/neverlish.go b/best-time-to-buy-and-sell-stock/neverlish.go new file mode 100644 index 000000000..101b67558 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/neverlish.go @@ -0,0 +1,34 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +package main + +import "testing" + +func TestMaxProfit(t *testing.T) { + prices := []int{7, 1, 5, 3, 6, 4} + if maxProfit(prices) != 5 { + t.Error("Test case 0 failed") + } + + prices = []int{7, 6, 4, 3, 1} + if maxProfit(prices) != 0 { + t.Error("Test case 1 failed") + } +} + +func maxProfit(prices []int) int { + minPrice := prices[0] + result := 0 + + for _, price := range prices { + profit := price - minPrice + if profit > result { + result = profit + } + if price < minPrice { + minPrice = price + } + } + return result +} diff --git a/encode-and-decode-strings/neverlish.go b/encode-and-decode-strings/neverlish.go new file mode 100644 index 000000000..1dd60e903 --- /dev/null +++ b/encode-and-decode-strings/neverlish.go @@ -0,0 +1,48 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +package main + +import ( + "bytes" + "fmt" + "strconv" + "testing" +) + +func TestEncodeAndDecode(t *testing.T) { + strs := []string{"abc", "def", "ghi"} + encoded := encode(strs) + decoded := decode(encoded) + if len(strs) != len(decoded) { + t.Errorf("Expected %v but got %v", strs, decoded) + } + for i := 0; i < len(strs); i++ { + if strs[i] != decoded[i] { + t.Errorf("Expected %v but got %v", strs, decoded) + } + } +} + +func encode(strs []string) string { + var buffer bytes.Buffer + for _, str := range strs { + buffer.WriteString(fmt.Sprintf("%d~", len(str))) + buffer.WriteString(str) + } + return buffer.String() +} + +func decode(str string) []string { + var result []string + for i := 0; i < len(str); { + j := i + for str[j] != '~' { + j++ + } + length, _ := strconv.Atoi(str[i:j]) + result = append(result, str[j+1:j+1+length]) + i = j + 1 + length + } + return result +} diff --git a/group-anagrams/neverlish.go b/group-anagrams/neverlish.go new file mode 100644 index 000000000..9b01117a7 --- /dev/null +++ b/group-anagrams/neverlish.go @@ -0,0 +1,49 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +package main + +import "testing" + +func TestGroupAnagrams(t *testing.T) { + strs := []string{"eat", "tea", "tan", "ate", "nat", "bat"} + result := groupAnagrams(strs) + + if len(result) != 3 { + t.Error("Test case 0 failed") + } + + if len(result[0]) != 3 { + t.Error("Test case 1 failed") + } + + if len(result[1]) != 3 { + t.Error("Test case 2 failed") + } + + if len(result[2]) != 1 { + t.Error("Test case 3 failed") + } +} + +func groupAnagrams(strs []string) [][]string { + result := make([][]string, 0) + + m := make(map[[26]int]int) + + for _, str := range strs { + key := [26]int{} + for _, c := range str { + key[c-'a']++ + } + + if idx, ok := m[key]; ok { + result[idx] = append(result[idx], str) + } else { + m[key] = len(result) + result = append(result, []string{str}) + } + } + + return result +} diff --git a/implement-trie-prefix-tree/neverlish.go b/implement-trie-prefix-tree/neverlish.go new file mode 100644 index 000000000..16ede79de --- /dev/null +++ b/implement-trie-prefix-tree/neverlish.go @@ -0,0 +1,54 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(n) + +package main + +type Trie struct { + children map[byte]*Trie + isEnd bool +} + +func Constructor() Trie { + return Trie{children: make(map[byte]*Trie)} +} + +func (this *Trie) Insert(word string) { + for i := 0; i < len(word); i++ { + if _, ok := this.children[word[i]]; !ok { + this.children[word[i]] = &Trie{children: make(map[byte]*Trie)} + } + this = this.children[word[i]] + } + this.isEnd = true +} + +func (this *Trie) Search(word string) bool { + for i := 0; i < len(word); i++ { + if _, ok := this.children[word[i]]; !ok { + return false + } + this = this.children[word[i]] + } + if this.isEnd { + return true + } + return false +} + +func (this *Trie) StartsWith(prefix string) bool { + for i := 0; i < len(prefix); i++ { + if _, ok := this.children[prefix[i]]; !ok { + return false + } + this = this.children[prefix[i]] + } + return true +} + +/** + * Your Trie object will be instantiated and called as such: + * obj := Constructor(); + * obj.Insert(word); + * param_2 := obj.Search(word); + * param_3 := obj.StartsWith(prefix); + */ diff --git a/word-break/neverlish.go b/word-break/neverlish.go new file mode 100644 index 000000000..7dae2912b --- /dev/null +++ b/word-break/neverlish.go @@ -0,0 +1,46 @@ +// 시간복잡도: O(n^2) +// 공간복잡도: O(n) + +package main + +import "testing" + +func TestWordBreak(t *testing.T) { + + result1 := wordBreak("leetcode", []string{"leet", "code"}) + if result1 != true { + t.Error("Test case 0 failed") + } + + result2 := wordBreak("applepenapple", []string{"apple", "pen"}) + if result2 != true { + t.Error("Test case 1 failed") + } + + result3 := wordBreak("catsandog", []string{"cats", "dog", "sand", "and", "cat"}) + if result3 != false { + t.Error("Test case 2 failed") + } +} + +func wordBreak(s string, wordDict []string) bool { + wordMap := make(map[string]bool) + + for _, word := range wordDict { + wordMap[word] = true + } + + dp := make([]bool, len(s)+1) + dp[0] = true + for i := 1; i <= len(s); i++ { + for j := 0; j < i; j++ { + word := s[j:i] + if dp[j] && wordMap[word] { + dp[i] = true + break + } + } + } + + return dp[len(s)] +}