-
-
Notifications
You must be signed in to change notification settings - Fork 245
[uraflower] WEEK 05 Solutions #1382
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dc5bd6f
[ PS ] : Best Time to Buy and Sell Stock
uraflower 04a1960
[ PS ] : Group Anagrams
uraflower 8b957b5
[ PS ] : Encode and Decode Strings
uraflower ce42aaa
[ PS ] : Implement Trie (Prefix Tree)
uraflower 6a37193
[ PS ] : Word Break
uraflower 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,18 @@ | ||
/** | ||
* 주어진 prices에서 가장 큰 prices[j] - prices[i] (i < j) 를 반환하는 함수 | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
const maxProfit = function(prices) { | ||
let min = prices[0]; | ||
let profit = 0; | ||
|
||
for (const price of prices) { | ||
min = Math.min(min, price); | ||
profit = Math.max(profit, price - min); | ||
} | ||
|
||
return profit; | ||
}; | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) |
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,16 @@ | ||
const encode = function (strs) { | ||
const separator = '\\'; | ||
return strs.join(separator); | ||
} | ||
|
||
|
||
const decode = function (str) { | ||
const separator = '\\'; | ||
return str.split(separator); | ||
} | ||
|
||
// 문제가 너무 별로다 | ||
// 문제에서 제시하고 있는 해답도 별로다 | ||
// 이모지같은 걸 구분자로 쓰거나, length를 앞에 넣어서 구별하거나 해도 사실 제대로 암호화했다고 말할 수 없음 | ||
// 그런 걸 정답으로 제공할 거면 이런 문제를 왜 내는 거여 | ||
// 이 문제가 Blind 75에 속해 있는 이유가 뭘까...?! | ||
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 @@ | ||
/** | ||
* 애너그램끼리 묶어서 반환하는 함수 | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
const groupAnagrams = function(strs) { | ||
// 풀이 1 | ||
// 시간복잡도: O(n*s) (n: strs.length, s: str.length) | ||
// 공간복잡도: O(n) | ||
function groupManually () { | ||
const groups = {}; | ||
|
||
strs.forEach((str) => { | ||
const key = [...str].sort().join(''); | ||
if (!groups[key]) groups[key] = []; | ||
groups[key].push(str); | ||
}); | ||
|
||
return Object.values(groups); | ||
} | ||
|
||
// 풀이 2 | ||
// 시간복잡도: O(n*s) (n: strs.length, s: str.length) | ||
// 공간복잡도: O(n) | ||
function groupByAnagram() { | ||
const result = Object.groupBy(strs, (str) => [...str].sort().join('')); | ||
return Object.values(result); | ||
} | ||
|
||
return groupByAnagram(); | ||
}; | ||
|
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,65 @@ | ||
const Node = function (value) { | ||
this.value = value; | ||
this.children = {}; | ||
this.data = null; | ||
} | ||
|
||
const Trie = function () { | ||
this.root = new Node(null); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
Trie.prototype.insert = function (word) { | ||
let parent = this.root; | ||
|
||
for (let i = 0; i < word.length; i++) { | ||
if (!parent.children[word[i]]) { | ||
parent.children[word[i]] = new Node(word[i]); | ||
} | ||
parent = parent.children[word[i]]; | ||
} | ||
|
||
parent.data = word; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.search = function (word) { | ||
let parent = this.root; | ||
let i = 0; | ||
|
||
while (i < word.length && parent.children[word[i]]) { | ||
parent = parent.children[word[i]]; | ||
i += 1; | ||
} | ||
|
||
return parent.data === word; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function (prefix) { | ||
let parent = this.root; | ||
|
||
for (let char of prefix) { | ||
if (!parent.children[char]) return false; | ||
parent = parent.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,30 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string[]} wordDict | ||
* @return {boolean} | ||
*/ | ||
const wordBreak = function (s, wordDict) { | ||
const dp = Array(s.length); | ||
const dict = new Set(wordDict); | ||
|
||
function recurse(start) { | ||
if (start === s.length) return true; | ||
if (dp[start] !== undefined) return dp[start]; | ||
|
||
for (let end = start + 1; end <= s.length; end++) { | ||
const substr = s.slice(start, end); | ||
if (dict.has(substr) && recurse(end)) { | ||
dp[start] = true; | ||
return true; | ||
} | ||
} | ||
|
||
dp[start] = false; | ||
return false; | ||
} | ||
|
||
return recurse(0); | ||
}; | ||
|
||
// 시간복잡도: O(n^2) (n: s.length. n번 재귀 & 최대 n번 슬라이싱) | ||
// 공간복잡도: O(n + m) (m: wordDict.length) |
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.
저도 공감합니다..!