-
Notifications
You must be signed in to change notification settings - Fork 126
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
neverlish
wants to merge
1
commit into
DaleStudy:main
Choose a base branch
from
neverlish:w5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−0
Open
[jinho] Week 5 #878
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 dfs를 생각했는데 dp를 사용했면 공간 복잡성도 줄일 수 있고 더 효율적인거 같아요! 좋은 코드 감사합니다 :)