-
-
Notifications
You must be signed in to change notification settings - Fork 339
[tedkimdev] WEEK 05 Solutions #2497
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
Merged
Merged
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 hidden or 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,20 @@ | ||
| // TC: O(n) | ||
| // SC: O(1) | ||
| func maxProfit(prices []int) int { | ||
| l, r := 0, 1 | ||
| max := 0 | ||
|
|
||
| for r < len(prices) { | ||
| if prices[l] < prices[r] { | ||
| profit := prices[r] - prices[l] | ||
| if profit > max { | ||
| max = profit | ||
| } | ||
| } else { | ||
| l = r | ||
| } | ||
| r++ | ||
| } | ||
|
|
||
| return max | ||
| } |
This file contains hidden or 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,32 @@ | ||
| type Solution struct{} | ||
|
|
||
| // TC: O(m) | ||
| // SC: O(m + n) | ||
| func (s *Solution) Encode(strs []string) string { | ||
| encoded := "" | ||
| for _, str := range strs { | ||
| encoded += fmt.Sprintf("%d|%s", len(str), str) | ||
| } | ||
|
|
||
| return encoded | ||
| } | ||
|
|
||
| // TC: O(m) | ||
| // SC: O(m + n) | ||
| func (s *Solution) Decode(encoded string) []string { | ||
| decoded := make([]string, 0) | ||
|
|
||
| i := 0 | ||
| for i < len(encoded) { | ||
| j := i | ||
| for encoded[j] != '|' { | ||
| j++ | ||
| } | ||
| length, _ := strconv.Atoi(encoded[i:j]) | ||
| i = j + 1 | ||
| decoded = append(decoded, encoded[i:i+length]) | ||
| i += length | ||
| } | ||
|
|
||
| return decoded | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 알파벳 자리수를 이용해서 풀이하셨네요. 좋습니다! 👍 |
This file contains hidden or 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,19 @@ | ||
| // TC: O(m * n) | ||
| // SC: O(m * n) | ||
| func groupAnagrams(strs []string) [][]string { | ||
| groups := map[[26]int][]string{} | ||
|
|
||
| for _, s := range strs { | ||
| var count [26]int | ||
| for _, c := range s { | ||
| count[c-'a']++ | ||
| } | ||
| groups[count] = append(groups[count], s) | ||
| } | ||
|
|
||
| result := [][]string{} | ||
| for _, group := range groups { | ||
| result = append(result, group) | ||
| } | ||
| return result | ||
| } |
This file contains hidden or 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,52 @@ | ||
| // TC: O(n) | ||
| // SC: O(t) - Where n is the length of the string and t is the total number of TrieNodes created in the Trie. | ||
| type PrefixTree struct { | ||
| children map[rune]*PrefixTree | ||
| isWord bool | ||
| } | ||
|
|
||
| func Constructor() PrefixTree { | ||
| return PrefixTree{ | ||
| children: map[rune]*PrefixTree{}, | ||
| isWord: false, | ||
| } | ||
| } | ||
|
|
||
| func (this *PrefixTree) Insert(word string) { | ||
| cur := this | ||
| for _, c := range word { | ||
| if _, ok := cur.children[c]; !ok { | ||
| child := Constructor() | ||
| cur.children[c] = &child | ||
| cur = cur.children[c] | ||
| } else { | ||
| cur = cur.children[c] | ||
| } | ||
| } | ||
| cur.isWord = true | ||
| } | ||
|
|
||
| func (this *PrefixTree) Search(word string) bool { | ||
| cur := this | ||
| for _, c := range word { | ||
| if _, ok := cur.children[c]; ok { | ||
| cur = cur.children[c] | ||
| } else { | ||
| return false | ||
| } | ||
| } | ||
| return cur.isWord | ||
| } | ||
|
|
||
| func (this *PrefixTree) StartsWith(prefix string) bool { | ||
| cur := this | ||
| for _, c := range prefix { | ||
| if _, ok := cur.children[c]; ok { | ||
| cur = cur.children[c] | ||
| } else { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return cur != nil | ||
| } |
This file contains hidden or 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,67 @@ | ||
| // TC: O((n * t) + (m * t)) | ||
| // SC: O(n + (m * t)) | ||
| func wordBreak(s string, wordDict []string) bool { | ||
| trie := NewTrieNode() | ||
| maxLength := 0 | ||
| for _, word := range wordDict { | ||
| trie.Insert(word) | ||
| if len(word) > maxLength { | ||
| maxLength = len(word) | ||
| } | ||
| } | ||
|
|
||
| dp := make([]bool, len(s)+1) | ||
| dp[len(s)] = true | ||
|
|
||
| for i := len(s) - 1; i >= 0; i-- { | ||
| node := trie | ||
| for j := i; j < len(s) && j < i+maxLength; j++ { | ||
| c := s[j] | ||
| if _, ok := node.children[rune(c)]; !ok { | ||
| break | ||
| } | ||
| node = node.children[rune(c)] | ||
|
|
||
| if node.isWord && dp[j+1] { | ||
| dp[i] = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return dp[0] | ||
| } | ||
|
|
||
| type TrieNode struct { | ||
| children map[rune]*TrieNode | ||
| isWord bool | ||
| } | ||
|
|
||
| func NewTrieNode() *TrieNode { | ||
| return &TrieNode{children: make(map[rune]*TrieNode)} | ||
| } | ||
|
|
||
| func (t *TrieNode) Insert(word string) { | ||
| node := t | ||
| for _, char := range word { | ||
| if _, ok := node.children[char]; !ok { | ||
| node.children[char] = NewTrieNode() | ||
| } | ||
| node = node.children[char] | ||
| } | ||
| node.isWord = true | ||
| } | ||
|
|
||
| // func (t *TrieNode) Search(s string, i, j int) bool { | ||
| // node := t | ||
| // for idx := i; idx <= j; idx++ { | ||
| // char := rune(s[idx]) | ||
| // if _, ok := node.children[char]; !ok { | ||
| // return false | ||
| // } | ||
| // node = node.children[char] | ||
| // } | ||
| // return node.isWord | ||
| // } | ||
|
|
||
|
|
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.
특별히 문제될 부분은 없을 것 같습니다.