diff --git a/best-time-to-buy-and-sell-stock/gitsunmin.ts b/best-time-to-buy-and-sell-stock/gitsunmin.ts new file mode 100644 index 000000000..a411f52d4 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/gitsunmin.ts @@ -0,0 +1,17 @@ +/** + * https://leetcode.com/problems/best-time-to-buy-and-sell-stock + * time complexity : O(n) + * space complexity : O(1) + */ + +function maxProfit(prices: number[]): number { + let maxProfit = 0; + let [minPrice] = prices; + + for (let price of prices) { + maxProfit = Math.max(maxProfit, price - minPrice); + minPrice = Math.min(minPrice, price); + } + + return maxProfit; +}; diff --git a/group-anagrams/gitsunmin.ts b/group-anagrams/gitsunmin.ts new file mode 100644 index 000000000..308a03f85 --- /dev/null +++ b/group-anagrams/gitsunmin.ts @@ -0,0 +1,17 @@ +/** + * https://leetcode.com/problems/group-anagrams + * time complexity : O(n * k log k) + * space complexity : O(n * k) + */ +function groupAnagrams(strs: string[]): string[][] { + const map = new Map(); + + for (const str of strs) { + const sortedStr = str.split("").sort().join(""); + + if (map.has(sortedStr)) map.get(sortedStr).push(str); + else map.set(sortedStr, [str]); + } + + return Array.from(map.values()); +};