Skip to content

Commit

Permalink
[jinho] week5 (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
neverlish authored Jan 11, 2025
1 parent 767c2f3 commit 845cdbd
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 0 deletions.
34 changes: 34 additions & 0 deletions best-time-to-buy-and-sell-stock/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}
48 changes: 48 additions & 0 deletions encode-and-decode-strings/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions group-anagrams/neverlish.go
Original file line number Diff line number Diff line change
@@ -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
}
54 changes: 54 additions & 0 deletions implement-trie-prefix-tree/neverlish.go
Original file line number Diff line number Diff line change
@@ -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);
*/
46 changes: 46 additions & 0 deletions word-break/neverlish.go
Original file line number Diff line number Diff line change
@@ -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)]
}

0 comments on commit 845cdbd

Please sign in to comment.