Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jinho] Week 5 #878

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 dfs를 생각했는데 dp를 사용했면 공간 복잡성도 줄일 수 있고 더 효율적인거 같아요! 좋은 코드 감사합니다 :)

Loading