-
-
Notifications
You must be signed in to change notification settings - Fork 195
[moonjonghoo] Week 05 Solutions #1400
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,24 @@ | ||
/** | ||
* @param {number[]} prices | ||
* @return {number} | ||
*/ | ||
var maxProfit = function (prices) { | ||
let minPrices = Infinity; | ||
let profit = 0; | ||
for (price of prices) { | ||
minPrices = Math.min(price, minPrices); | ||
profit = Math.max(profit, price - minPrices); | ||
} | ||
return profit; | ||
}; | ||
|
||
var maxProfit = function (prices) { | ||
let maxProfit = 0; | ||
let currentProfit = 0; | ||
for (let i = 1; i < prices.length; i++) { | ||
let diff = prices[i] - prices[i - 1]; | ||
currentProfit = Math.max(0, currentProfit + diff); | ||
maxProfit = Math.max(maxProfit, currentProfit); | ||
} | ||
return maxProfit; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* @param {string[]} strs | ||
* @return {string[][]} | ||
*/ | ||
var groupAnagrams = function (strs) { | ||
let hashMap = new Map(); | ||
for (let i = 0; i < strs.length; i++) { | ||
let key = strs[i].split("").sort().join(""); | ||
if (!hashMap.has(key)) { | ||
hashMap.set(key, [strs[i]]); | ||
} else { | ||
hashMap.set(key, [...hashMap.get(key), strs[i]]); | ||
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.
을 활용하면 새 배열을 생성하지 않고 바로 push 할 수 있습니다. 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. 아 맞습니다. 반영을 못했네요 ㅠㅠ |
||
} | ||
} | ||
let answer = []; | ||
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. 오 그러네요 감사합니다. |
||
for ([key, value] of hashMap) { | ||
answer.push(value); | ||
} | ||
return answer.sort((a, b) => a.length - b.length).map((arr) => arr.sort()); | ||
}; | ||
|
||
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string[]} wordDict | ||
* @return {boolean} | ||
*/ | ||
var wordBreak = function (s, wordDict) { | ||
const memo = new Map(); // 실패/성공 여부 기억 | ||
const wordSet = new Set(wordDict); // lookup 최적화 | ||
|
||
function canBreak(sub) { | ||
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 (sub === "") return true; | ||
|
||
if (memo.has(sub)) return memo.get(sub); | ||
|
||
for (let i = 1; i <= sub.length; i++) { | ||
const prefix = sub.slice(0, i); | ||
if (wordSet.has(prefix)) { | ||
const suffix = sub.slice(i); | ||
if (canBreak(suffix)) { | ||
memo.set(sub, true); | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
memo.set(sub, false); // 실패한 경우도 기억 | ||
return false; | ||
} | ||
|
||
return canBreak(s); | ||
}; |
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.
어제와 오늘의 가격 차이를 계산하고 그 합이 최대가 되는 구간을 찾는 로직이군여!
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.
맞습니다!