-
-
Notifications
You must be signed in to change notification settings - Fork 195
[lhc0506] WEEK 05 solutions #1395
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function(prices) { | ||
let minPrice = Number.MAX_SAFE_INTEGER; | ||
let maxProfit = 0; | ||
|
||
for (let i = 0; i < prices.length; i++) { | ||
minPrice = Math.min(minPrice, prices[i]); | ||
maxProfit = Math.max(maxProfit, prices[i] - minPrice); | ||
} | ||
|
||
return maxProfit; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
/** | ||
* @param {string[]} strs | ||
* @returns {string} | ||
*/ | ||
encode(strs) { | ||
return JSON.stringify(strs); | ||
} | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {string[]} | ||
*/ | ||
decode(str) { | ||
return JSON.parse(str); | ||
} | ||
} |
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. 정렬 없이 문제 풀으신 분은 처음봤습니다; 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. 문자열 배열내 각 문자열 길이는 정해지지않았으니 평균을 l로 하면 O(n * l) 이 더 정확할 것 같습니다. 감사합니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
var groupAnagrams = function(strs) { | ||
const A_ASCII = 'a'.charCodeAt(); | ||
|
||
const anagramMap = new Map(); | ||
|
||
for (const str of strs) { | ||
const counter = new Array(26).fill(0); | ||
|
||
for (let i = 0; i < str.length; i++) { | ||
counter[str.charCodeAt(i) - A_ASCII]++; | ||
} | ||
|
||
let key = ''; | ||
for (let i = 0; i < 26; i++) { | ||
if (counter[i] > 0) { | ||
key += String.fromCharCode(i + A_ASCII) + counter[i]; | ||
} | ||
} | ||
|
||
if (!anagramMap.has(key)) { | ||
anagramMap.set(key, []); | ||
} | ||
anagramMap.get(key).push(str); | ||
} | ||
|
||
return Array.from(anagramMap.values()); | ||
}; | ||
|
||
// 시간 복잡도: O(n), 공간 복잡도: O(n) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
var Trie = function() { | ||
this.isEnd = false; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
Trie.prototype.insert = function(word) { | ||
let currentTrie = this; | ||
word.split('').forEach(alphabet => { | ||
if (!currentTrie[alphabet]) { | ||
currentTrie[alphabet] = new Trie(); | ||
} | ||
currentTrie = currentTrie[alphabet]; | ||
}); | ||
|
||
currentTrie.isEnd = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.search = function(word) { | ||
let currentTrie = this; | ||
|
||
for (let alphabet of word) { | ||
if (!currentTrie[alphabet]) { | ||
return false; | ||
} | ||
currentTrie = currentTrie[alphabet]; | ||
} | ||
|
||
return currentTrie.isEnd; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function(prefix) { | ||
let currentTrie = this; | ||
|
||
for (let alphabet of prefix) { | ||
if (!currentTrie[alphabet]) { | ||
return false; | ||
} | ||
currentTrie = currentTrie[alphabet]; | ||
} | ||
|
||
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) | ||
*/ | ||
|
||
|
||
// insert 시간복잡도: O(n), search 시간복잡도: O(n), startsWith 시간복잡도: O(n) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string[]} wordDict | ||
* @return {boolean} | ||
*/ | ||
var wordBreak = function(s, wordDict) { | ||
const wordDictSet = new Set(wordDict); | ||
const visited = new Set(); | ||
|
||
const indexStack = [0]; | ||
while(indexStack.length > 0) { | ||
const startIndex = indexStack.pop(); | ||
|
||
if (visited.has(startIndex)) continue; | ||
visited.add(startIndex); | ||
|
||
if (startIndex === s.length) return true; | ||
|
||
for (let endIndex = startIndex + 1; endIndex <= s.length; endIndex++) { | ||
const word = s.substring(startIndex, endIndex); | ||
|
||
if (wordDictSet.has(word)) { | ||
indexStack.push(endIndex); | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
}; |
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.
JSON.stringfy()
문법을 쓰시는 것도 좋지만, 면접관 입장에서는 내부 동작 방식을 설명하거나 내장 함수 사용 없이 풀어보라고 할 수도 있어서요! 내장함수 사용 없이 풀어보시면 또 다른 방법을 찾으실 수있을것 같아요 ㅎㅎ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.
각 문자 알파벳을 ASCII코드로 바꾸고 분리를 시키는것도 생각해봤긴했는데 다음엔 두개다 써보도록 하겠습니다. 감사합니다~