Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gitsunmin] Week 5 Solutions #461

Merged
merged 3 commits into from
Sep 16, 2024
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());
};