Skip to content

Commit 7acbc80

Browse files
authored
Merge pull request #2505 from DaleStudy/yihyun-kim1
[yihyun-kim1] WEEK 05 Solutions
2 parents aa8dfde + 2b976cd commit 7acbc80

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
const maxProfit = (prices) => {
6+
let minPrice = prices[0];
7+
let maxProfit = 0;
8+
9+
for (let i = 1; i < prices.length; i++) {
10+
minPrice = Math.min(minPrice, prices[i]);
11+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
12+
}
13+
14+
return maxProfit;
15+
};

group-anagrams/yihyun-kim1.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
const groupAnagrams = (strs) => {
6+
const map = new Map();
7+
8+
for (const str of strs) {
9+
const sorted = str.split("").sort().join("");
10+
11+
if (!map.has(sorted)) {
12+
map.set(sorted, []);
13+
}
14+
map.get(sorted).push(str);
15+
}
16+
17+
return [...map.values()];
18+
};

word-break/yihyun-kim1.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {string} s
3+
* @param {string[]} wordDict
4+
* @return {boolean}
5+
*/
6+
const wordBreak = (s, wordDict) => {
7+
const dp = new Array(s.length + 1).fill(false);
8+
dp[0] = true;
9+
10+
for (let i = 1; i <= s.length; i++) {
11+
for (const word of wordDict) {
12+
const start = i - word.length;
13+
if (start >= 0 && dp[start] && s.slice(start, i) === word) {
14+
dp[i] = true;
15+
}
16+
}
17+
}
18+
19+
return dp[s.length];
20+
};

0 commit comments

Comments
 (0)