diff --git a/best-time-to-buy-and-sell-stock/jun0811.js b/best-time-to-buy-and-sell-stock/jun0811.js new file mode 100644 index 0000000000..e2e41c5bf9 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/jun0811.js @@ -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; +}; diff --git a/group-anagrams/jun0811.js b/group-anagrams/jun0811.js new file mode 100644 index 0000000000..26275e0771 --- /dev/null +++ b/group-anagrams/jun0811.js @@ -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; +};