Skip to content

Commit 392d491

Browse files
authored
Merge pull request #2499 from sadie100/main
[sadie100] WEEK 05 Solutions
2 parents 7a067f7 + 53ba8fc commit 392d491

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
prices๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ํ˜„์žฌ ์ตœ์ €๊ฐ’๊ณผ์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•ด์„œ ์ตœ๋Œ€ profit์„ ๊ฐฑ์‹ ํ•œ ๋’ค ์ตœ์ €๊ฐ’(๊ตฌ๋งค๊ฐ€)๋ฅผ ๊ฐฑ์‹ , ์ตœ์ข…๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค
3+
4+
์‹œ๊ฐ„๋ณต์žก๋„ O(N) - N์€ prices์˜ length
5+
*/
6+
7+
function maxProfit(prices: number[]): number {
8+
let buy
9+
let result = 0
10+
11+
for (let price of prices) {
12+
if (buy === undefined) {
13+
buy = price
14+
continue
15+
}
16+
result = Math.max(result, price - buy)
17+
buy = Math.min(price, buy)
18+
}
19+
20+
return result
21+
}

โ€Žgroup-anagrams/sadie100.tsโ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
strs์˜ ๋ฌธ์ž์—ด๋“ค์„ ๋“ฑ์žฅ๋นˆ๋„๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ณ„์‚ฐํ•œ ํŠน์ˆ˜ ๋ฌธ์ž์—ด๋กœ ์ „ํ™˜ํ•˜๊ณ  ๊ฐ™์€ ๊ฒƒ๋ผ๋ฆฌ ๋ฌถ๋Š”๋‹ค.
3+
4+
์‹œ๊ฐ„๋ณต์žก๋„ : O(N * K) - N์€ strs์˜ ๊ฐœ์ˆ˜, K๋Š” strs์˜ ๊ธธ์ด. N ์ˆœํšŒ ์•ˆ์— K ์ˆœํšŒ
5+
*/
6+
7+
function groupAnagrams(strs: string[]): string[][] {
8+
const anagramMap = {}
9+
const result = strs.reduce((acc, cur) => {
10+
const charArray = new Array(26).fill(0)
11+
12+
for (let char of cur) {
13+
const charIdx = char.charCodeAt(0) - 'a'.charCodeAt(0)
14+
charArray[charIdx] += 1
15+
}
16+
17+
const sortedValue = charArray.join('#')
18+
19+
const targetIdx = anagramMap[sortedValue]
20+
if (targetIdx !== undefined) {
21+
acc[targetIdx].push(cur)
22+
} else {
23+
anagramMap[sortedValue] = acc.length
24+
acc.push([cur])
25+
}
26+
return acc
27+
}, [])
28+
29+
return result
30+
}

0 commit comments

Comments
ย (0)