-
-
Notifications
You must be signed in to change notification settings - Fork 195
[khyo] Week 5 #880
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
[khyo] Week 5 #880
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,16 @@ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function(prices) { | ||
let answer = 0 | ||
let leftMin = prices[0] | ||
|
||
for(let i = 1; i < prices.length; ++i) { | ||
answer = Math.max(answer, prices[i] - leftMin); | ||
leftMin = Math.min(prices[i], leftMin); | ||
} | ||
|
||
return answer; | ||
}; | ||
|
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,62 @@ | ||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
* | ||
* complexity | ||
* time: O(n * m) | ||
* space: O(n) | ||
* | ||
* 풀이 | ||
* 처음에는 각 알파벳에 숫자를 할당하여 합을 이용해서 풀이하는 방식으로 접근하려고 했으나, | ||
* 합의 경우의 수가 너무 많아서 중복되는 경우가 생겨서 다른 풀이 방식을 생각했다. | ||
* -> 이후 소수를 이용한 풀이를 생각했다. (소수의 곱을 이용하는 경우 중복되는 경우가 없다.) | ||
* 하지만 최악의 경우 소수의 곱이 너무 커져서 오버플로우가 발생한다. | ||
* | ||
* 그래서 정렬을 통해 각 문자열을 정렬하여 키로 사용하는 방식으로 접근했다. | ||
*/ | ||
var groupAnagrams = function(strs) { | ||
const map = new Map(); | ||
|
||
for(const str of strs) { | ||
const sortedStr = str.split('').sort().join(''); | ||
|
||
if(!map.has(sortedStr)) { | ||
map.set(sortedStr, []); | ||
} | ||
map.get(sortedStr).push(str); | ||
} | ||
|
||
return Array.from(map.values()); | ||
}; | ||
|
||
/** | ||
* passed 되었으나, 최악의 경우에는 통과될 수 없지 않을까? | ||
* | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
* | ||
* complexity | ||
* time: O(n * m) | ||
* space: O(n) | ||
*/ | ||
var groupAnagrams = function(strs) { | ||
const map = new Map(); | ||
|
||
const primes = [2,3,5,7,11,13,17,19,23,29,31,37,41, | ||
43,47,53,59,61,67,71,73,79,83,89,97,101]; | ||
|
||
for(const str of strs) { | ||
let key = 1; | ||
for(const char of str) { | ||
key *= primes[char.charCodeAt(0) - 'a'.charCodeAt(0)]; | ||
} | ||
|
||
if(!map.has(key)) { | ||
map.set(key, []); | ||
} | ||
map.get(key).push(str); | ||
} | ||
|
||
return Array.from(map.values()); | ||
}; | ||
|
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,63 @@ | ||
var Trie = function() { | ||
this.root = {}; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
* | ||
* complexity | ||
* time: O(n) | ||
* space: O(n) | ||
*/ | ||
Trie.prototype.insert = function(word) { | ||
let cur = this.root; | ||
for (let x of word) { | ||
if (!cur[x]) cur[x] = {}; | ||
cur = cur[x]; | ||
} | ||
cur.end = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
* | ||
* complexity | ||
* time: O(n) | ||
* space: O(1) | ||
*/ | ||
Trie.prototype.search = function(word) { | ||
let cur = this.find(word); | ||
return cur !== null && cur.end === true; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
* | ||
* complexity | ||
* time: O(n) | ||
* space: O(1) | ||
*/ | ||
Trie.prototype.startsWith = function(prefix) { | ||
return this.find(prefix) !== null; | ||
}; | ||
|
||
/** | ||
* @param {string} str | ||
* @return {object} | ||
* | ||
* complexity | ||
* time: O(n) | ||
* space: O(1) | ||
*/ | ||
Trie.prototype.find = function(str) { | ||
let cur = this.root; | ||
for (let x of str) { | ||
if (!cur[x]) return null; | ||
cur = cur[x]; | ||
} | ||
return cur; | ||
}; | ||
|
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.
이 라인이 시간복잡도 측면에서 핵심인 것 같은데, 다른 방식으로 key를 생성하면 시간복잡도를 줄일 수 있을 것 같습니다. 예를 들어 알파벳이니까 굳이 정렬을 안해도 a-z의 빈도를 나타내는 문자열로 변환하거나 할 수 있을 것 같습니다.
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.
피드백 감사합니다!