Skip to content

Commit

Permalink
Merge pull request #461 from gitsunmin/main
Browse files Browse the repository at this point in the history
[gitsunmin] Week 5 Solutions
  • Loading branch information
SamTheKorean authored Sep 16, 2024
2 parents 9cc99d4 + 4ad8bea commit 486d50b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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());
};

0 comments on commit 486d50b

Please sign in to comment.