Skip to content

Commit 486d50b

Browse files
authored
Merge pull request #461 from gitsunmin/main
[gitsunmin] Week 5 Solutions
2 parents 9cc99d4 + 4ad8bea commit 486d50b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock
3+
* time complexity : O(n)
4+
* space complexity : O(1)
5+
*/
6+
7+
function maxProfit(prices: number[]): number {
8+
let maxProfit = 0;
9+
let [minPrice] = prices;
10+
11+
for (let price of prices) {
12+
maxProfit = Math.max(maxProfit, price - minPrice);
13+
minPrice = Math.min(minPrice, price);
14+
}
15+
16+
return maxProfit;
17+
};

group-anagrams/gitsunmin.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* https://leetcode.com/problems/group-anagrams
3+
* time complexity : O(n * k log k)
4+
* space complexity : O(n * k)
5+
*/
6+
function groupAnagrams(strs: string[]): string[][] {
7+
const map = new Map();
8+
9+
for (const str of strs) {
10+
const sortedStr = str.split("").sort().join("");
11+
12+
if (map.has(sortedStr)) map.get(sortedStr).push(str);
13+
else map.set(sortedStr, [str]);
14+
}
15+
16+
return Array.from(map.values());
17+
};

0 commit comments

Comments
 (0)