-
-
Notifications
You must be signed in to change notification settings - Fork 194
[hsskey] Week 05 Solutions #1389
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,19 @@ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function(prices) { | ||
let buy = prices[0] | ||
let maxVal = 0 | ||
|
||
for(let i = 1; i < prices.length; i++) { | ||
if(prices[i - 1] > prices[i]) { | ||
buy = Math.min(buy, prices[i]) | ||
} | ||
|
||
if(prices[i - 1] < prices[i]) { | ||
maxVal = Math.max(maxVal, prices[i] - buy) | ||
} | ||
} | ||
return maxVal | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Solution { | ||
/** | ||
* @param {string[]} strs | ||
* @returns {string} | ||
*/ | ||
encode(strs) { | ||
return strs.map((item) => `${item.length}#${item}`).join(''); | ||
} | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {string[]} | ||
*/ | ||
decode(str) { | ||
const result = []; | ||
let i = 0; | ||
|
||
while (i < str.length) { | ||
let j = i; | ||
while (str[j] !== '#') { | ||
j++; | ||
} | ||
|
||
const length = parseInt(str.slice(i, j)); | ||
const word = str.slice(j + 1, j + 1 + length); | ||
result.push(word); | ||
|
||
i = j + 1 + length; | ||
} | ||
|
||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
var groupAnagrams = function(strs) { | ||
const map = new Map() | ||
|
||
for(const str of strs) { | ||
const sortedStr = [...str].sort().join('') | ||
if(map.has(sortedStr)) { | ||
map.get(sortedStr).push(str) | ||
} else { | ||
map.set(sortedStr, [str]) | ||
} | ||
} | ||
|
||
return Array.from(map.values()) | ||
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. 알파벳 빈도수 배열로 그룹화하는 방식으로도 풀 수 있는 방법을 알게 되서 공유드려요
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class TrieNode { | ||
constructor() { | ||
this.children = {}; | ||
this.isEndOfWord = false; | ||
} | ||
} | ||
|
||
var Trie = function() { | ||
this.root = new TrieNode(); | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {void} | ||
*/ | ||
Trie.prototype.insert = function(word) { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node.children[char]) { | ||
node.children[char] = new TrieNode(); | ||
} | ||
node = node.children[char]; | ||
} | ||
node.isEndOfWord = true; | ||
}; | ||
|
||
/** | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.search = function(word) { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node.children[char]) return false; | ||
node = node.children[char]; | ||
} | ||
return node.isEndOfWord; | ||
}; | ||
|
||
/** | ||
* @param {string} prefix | ||
* @return {boolean} | ||
*/ | ||
Trie.prototype.startsWith = function(prefix) { | ||
let node = this.root; | ||
for (const char of prefix) { | ||
if (!node.children[char]) return false; | ||
node = node.children[char]; | ||
} | ||
return true; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string[]} wordDict | ||
* @return {boolean} | ||
*/ | ||
var wordBreak = function(s, wordDict) { | ||
const wordSet = new Set(wordDict); | ||
const dp = new Array(s.length + 1).fill(false); | ||
dp[0] = true; | ||
|
||
for (let i = 1; i <= s.length; i++) { | ||
for (let j = 0; j < i; j++) { | ||
const word = s.slice(j, i); | ||
if (dp[j] && wordSet.has(word)) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dp[s.length]; | ||
}; |
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.
인접한 가격(prices[i-1], prices[i])을 비교하는 방식보다 더 간단하게 접근할 수 있을 것 같아요. 현재 가격이 지금까지의 최소값보다 크면 이익을 계산하고, 아니면 최소값을 갱신하는 방식으로요!!
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.
지금 로직도 성능적으로는 O(n) 시간 복잡도와 O(1) 공간 복잡도를 유지해서 효율적이네요 👍
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.
직관적으로 생각해서 풀이했던 내용인데 피드백 주신내용으로 다시 풀어봐야겠네요 감사합니다.