-
-
Notifications
You must be signed in to change notification settings - Fork 339
[robinyoon-dev] WEEK 05 Solutions #2495
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
Open
robinyoon-dev
wants to merge
5
commits into
DaleStudy:main
Choose a base branch
from
robinyoon-dev:main
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.
+140
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
29fcc23
best time to buy and sell stock solution
robinyoon-dev 823887c
group anagrams solution
robinyoon-dev e7a77c7
implement trie prefix tree solution
robinyoon-dev 75a2e54
word break solution
robinyoon-dev 3679244
chore: add comment
robinyoon-dev 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
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,45 @@ | ||
| /** | ||
| * @param {string[]} strs | ||
| * @return {string[][]} | ||
| */ | ||
|
|
||
| // 문제는 풀었으나 시간 복잡도 측면에서 효율이 너무 떨어지는 풀이 방법.... | ||
| var groupAnagrams = function (strs) { | ||
| let outputArr = []; | ||
| let countArr = []; | ||
|
|
||
| const A_ASCII = 'a'.charCodeAt(0); | ||
| const Z_ASCII = 'z'.charCodeAt(0); | ||
|
|
||
| let charCounts = Z_ASCII - A_ASCII + 1; | ||
| let charCountArr = new Array(charCounts).fill(0); //인덱스가 알파벳을 나타냄. | ||
|
|
||
| for (str of strs) { | ||
| let strCountString = getStrCountString(str); | ||
|
|
||
| let hasSameCountIndex = countArr.findIndex((item) => item === strCountString); | ||
|
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. |
||
|
|
||
| if (hasSameCountIndex !== -1) { | ||
| outputArr[hasSameCountIndex].push(str); | ||
| } else { | ||
| countArr.push(strCountString); | ||
|
|
||
| outputArr.push([str]); | ||
| } | ||
| } | ||
|
|
||
| return outputArr; | ||
|
|
||
| function getStrCountString(str) { | ||
| let tempArr = [...charCountArr]; | ||
|
|
||
| for (char of str) { | ||
| let charAscii = char.charCodeAt(0); | ||
| let charIndex = charAscii - A_ASCII; | ||
| tempArr[charIndex] += 1; | ||
| } | ||
| return tempArr.join(','); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
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,68 @@ | ||
| // 문제풀이 해설 보고 푼 문제입니다. | ||
|
|
||
| var TrieNode = function () { | ||
| this.children = {}; | ||
| this.isEnd = false; | ||
| } | ||
|
|
||
| var Trie = function () { | ||
| this.root = new TrieNode(); | ||
| }; | ||
|
|
||
| /** | ||
| * @param {string} word | ||
| * @return {void} | ||
| */ | ||
| Trie.prototype.insert = function (word) { | ||
| let currentNode = this.root; | ||
| for (let i = 0; i < word.length; i++) { | ||
| let char = word[i]; | ||
| if (!currentNode.children[char]) { | ||
| currentNode.children[char] = new TrieNode(); | ||
|
|
||
| } | ||
| currentNode = currentNode.children[char]; | ||
| } | ||
|
|
||
| currentNode.isEnd = true; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {string} word | ||
| * @return {boolean} | ||
| */ | ||
| Trie.prototype.search = function (word) { | ||
| let currentNode = this.root; | ||
| for (let i = 0; i < word.length; i++) { | ||
| let char = word[i]; | ||
| if (!currentNode.children[char]) { | ||
| return false; | ||
| } | ||
| currentNode = currentNode.children[char]; | ||
| } | ||
| return currentNode.isEnd; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {string} prefix | ||
| * @return {boolean} | ||
| */ | ||
| Trie.prototype.startsWith = function (prefix) { | ||
| let currentNode = this.root; | ||
| for (let i = 0; i < prefix.length; i++) { | ||
| let char = prefix[i]; | ||
| if (!currentNode.children[char]) { | ||
| return false; | ||
| } | ||
| currentNode = currentNode.children[char]; | ||
| } | ||
| return true; | ||
| }; | ||
|
|
||
| /** | ||
| * Your Trie object will be instantiated and called as such: | ||
| * var obj = new Trie() | ||
| * obj.insert(word) | ||
| * var param_2 = obj.search(word) | ||
| * var param_3 = obj.startsWith(prefix) | ||
| */ |
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,27 @@ | ||
| // 문풀 해설 보고 푼 문제 | ||
| /** | ||
| * @param {string} s | ||
| * @param {string[]} wordDict | ||
| * @return {boolean} | ||
| */ | ||
| var wordBreak = function (s, wordDict) { | ||
|
|
||
| const dp = new Array(s.length + 1).fill(false); | ||
| dp[0] = true; | ||
|
|
||
| let maxLength = 0; | ||
| for (let word of wordDict) { | ||
| maxLength = Math.max(maxLength, word.length); | ||
| } | ||
|
|
||
| for (let i = 1; i <= s.length; i++) { | ||
| for (let j = Math.max(0, i - maxLength); j < i; j++) { | ||
| if (dp[j] && wordDict.includes(s.substring(j, i))) { | ||
| dp[i] = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return dp[s.length]; | ||
| }; |
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.
각 문자열을 알파벳 카운트 배열로 바꿔서 비교하는 방식 자체가 정석적인 anagram 풀이 아이디어라서, 접근을 잘 잡으신 것 같아요!