Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions best-time-to-buy-and-sell-stock/yihyun-kim1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @param {number[]} prices
* @return {number}
*/
const maxProfit = (prices) => {
let minPrice = prices[0];
let maxProfit = 0;

for (let i = 1; i < prices.length; i++) {
minPrice = Math.min(minPrice, prices[i]);
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
}

return maxProfit;
};
18 changes: 18 additions & 0 deletions group-anagrams/yihyun-kim1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {string[]} strs
* @return {string[][]}
*/
const groupAnagrams = (strs) => {
const map = new Map();

for (const str of strs) {
const sorted = str.split("").sort().join("");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(JS를 모르는 제 눈에는) 신기한 부분이네요! 문자열을 배열로 쪼개서 정렬한 다음, 다시 합친다는 개념이군요. 그리고 정렬된 문자열을 맵에 넣어서 문제를 해결하셨네요. 깔끔해보입니다.


if (!map.has(sorted)) {
map.set(sorted, []);
}
map.get(sorted).push(str);
}

return [...map.values()];
};
20 changes: 20 additions & 0 deletions word-break/yihyun-kim1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
const wordBreak = (s, wordDict) => {
const dp = new Array(s.length + 1).fill(false);
dp[0] = true;

for (let i = 1; i <= s.length; i++) {
for (const word of wordDict) {
const start = i - word.length;
if (start >= 0 && dp[start] && s.slice(start, i) === word) {
dp[i] = true;
}
}
}

return dp[s.length];
};
Loading