Skip to content

Commit 4b77d25

Browse files
authored
Merge pull request #1842 from jun0811/main
[jun0811] week05 solutions
2 parents 6b27756 + e1ae708 commit 4b77d25

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var maxProfit = function (prices) {
2+
let minPrice = prices[0];
3+
let maxProfit = 0;
4+
5+
for (let i = 1; i < prices.length; i++) {
6+
minPrice = Math.min(minPrice, prices[i]);
7+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
8+
}
9+
10+
return maxProfit;
11+
};

group-anagrams/jun0811.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function (strs) {
6+
const hashMap = new Map();
7+
const res = [];
8+
strs.forEach((str, index) => {
9+
const sortedStr = [...str].sort().join('');
10+
if (hashMap.has(sortedStr)) {
11+
hashMap.set(sortedStr, [...hashMap.get(sortedStr), index]);
12+
} else {
13+
hashMap.set(sortedStr, [index]);
14+
}
15+
});
16+
for (const [key, values] of hashMap) {
17+
const anagrams = values.map((v) => strs[v]);
18+
res.push(anagrams);
19+
}
20+
return res;
21+
};

0 commit comments

Comments
 (0)