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
17 changes: 17 additions & 0 deletions best-time-to-buy-and-sell-stock/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -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;
};
17 changes: 17 additions & 0 deletions group-anagrams/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -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());
};