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
11 changes: 11 additions & 0 deletions best-time-to-buy-and-sell-stock/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var maxProfit = function (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;
};
21 changes: 21 additions & 0 deletions group-anagrams/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
const hashMap = new Map();
const res = [];
strs.forEach((str, index) => {
const sortedStr = [...str].sort().join('');
if (hashMap.has(sortedStr)) {
hashMap.set(sortedStr, [...hashMap.get(sortedStr), index]);
} else {
hashMap.set(sortedStr, [index]);
}
});
for (const [key, values] of hashMap) {
const anagrams = values.map((v) => strs[v]);
res.push(anagrams);
}
return res;
};