Skip to content

Commit 81ba9c5

Browse files
authored
Merge pull request #855 from choidabom/main
[choidabom] Week 5
2 parents a267cd5 + efdefa1 commit 81ba9c5

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
3+
* Runtime: 6ms, Memory: 62.30MB
4+
*
5+
* Time Complexity: O(N)
6+
* Space Complexity: O(1)
7+
*/
8+
9+
function maxProfit(prices: number[]): number {
10+
let buyingPrice = prices[0];
11+
let profit = 0;
12+
13+
for (const price of prices) {
14+
buyingPrice = Math.min(price, buyingPrice);
15+
profit = Math.max(profit, price - buyingPrice);
16+
}
17+
18+
return profit;
19+
}
20+
21+
maxProfit([7, 1, 5, 3, 6, 4]);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* https://www.lintcode.com/problem/659/
3+
* Time Complexity: O(n)
4+
* Space Complexity: O(n)
5+
*/
6+
7+
function encode(strs: string[]): string {
8+
return strs.join("๐Ÿ˜€");
9+
}
10+
11+
function decode(str: string): string[] {
12+
return str.split("๐Ÿ˜€");
13+
}
14+
15+
decode(encode(["Hello", "World"]));
16+
/*
17+
* output
18+
* ["Hello","World"]
19+
*/

โ€Žgroup-anagrams/choidabom.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* https://leetcode.com/problems/group-anagrams/description/
3+
* Runtime: 27ms, Memory: 64.26MB
4+
*
5+
* Time Complexity: O(n * m * log m)
6+
* Space Complexity: O(n * m)
7+
*
8+
* ์ ‘๊ทผ
9+
* anagrams ๊ด€๊ณ„์ธ ๋‹จ์–ด๋Š” ์ •๋ ฌ ์‹œ ๋ชจ๋‘ ๋™์ผํ•œ ๋‹จ์–ด๊ฐ€ ๋œ๋‹ค.
10+
* ๊ทธ๋Ÿฌ๋ฏ€๋กœ ์ •๋ ฌ๋œ ๋‹จ์–ด๊ฐ€ key ๊ฐ’์ด ๋˜๊ณ , str์„ ์ •๋ ฌํ–ˆ์„ ๋•Œ key๊ฐ’๊ณผ ๋™์ผํ•œ ๊ฒฝ์šฐ value๊ฐ€ ๋˜๋„๋ก ์ถ”๊ฐ€.
11+
*/
12+
13+
function groupAnagrams(strs: string[]): string[][] {
14+
let answer = [];
15+
const anagramMap = new Map<string, string[]>();
16+
17+
for (let str of strs) {
18+
const key = str.split("").sort().join(""); // O(m * log m)
19+
if (!anagramMap.has(key)) {
20+
anagramMap.set(key, []);
21+
}
22+
anagramMap.get(key)?.push(str);
23+
}
24+
25+
anagramMap.forEach((value) => {
26+
answer.push(value);
27+
});
28+
29+
return answer;
30+
}
31+
32+
groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]);

0 commit comments

Comments
ย (0)