Skip to content

Commit 4c4c415

Browse files
authored
Merge pull request #2507 from kangdaia/main
[kangdaia] WEEK 05 solutions
2 parents 392d491 + a7790ff commit 4c4c415

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def maxProfit(self, prices: list[int]) -> int:
3+
"""
4+
์ฃผ์‹ ๊ฐ€๊ฒฉ ๋ชฉ๋ก์—์„œ ์ตœ๋Œ€ ์ด์ต์„ ๊ณ„์‚ฐํ•˜๋Š” ํ•จ์ˆ˜.
5+
6+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(N)
7+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
8+
9+
Args:
10+
prices (list[int]): ์ฃผ์‹ ๊ฐ€๊ฒฉ ๋ชฉ๋ก
11+
12+
Returns:
13+
int: ์ตœ๋Œ€ ์ด์ต
14+
"""
15+
min_price = prices[0]
16+
max_profit = 0
17+
for price in prices[1:]:
18+
max_profit = max(max_profit, price - min_price)
19+
min_price = min(min_price, price)
20+
return max_profit
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def encode(self, strs: list[str]) -> str:
3+
"""
4+
์ธ์ฝ”๋”ฉ ํ•จ์ˆ˜
5+
๋ฐฉ์‹: %๋ฅผ ์‚ฌ์šฉํ•ด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด + % + ๋ฌธ์ž์—ด๋กœ ์ธ์ฝ”๋”ฉ
6+
7+
Args:
8+
strs (list[str]): ์ธ์ฝ”๋”ฉํ•  ๋ฌธ์ž์—ด ๋ชฉ๋ก
9+
10+
Returns:
11+
str: ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด
12+
"""
13+
return "".join(f"{len(s)}%{s}" for s in strs)
14+
15+
def decode(self, s: str) -> list[str]:
16+
"""
17+
๋””์ฝ”๋”ฉ ํ•จ์ˆ˜
18+
์‹œ๊ฐ„๋ณต์žก๋„: O(N)
19+
20+
Args:
21+
s (str): ๋””์ฝ”๋”ฉํ•  ๋ฌธ์ž์—ด
22+
23+
Returns:
24+
list[str]: ๋””์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด ๋ชฉ๋ก
25+
"""
26+
res = []
27+
i = 0
28+
while i < len(s):
29+
j = i
30+
while s[j] != "%":
31+
j += 1
32+
length = int(s[i:j])
33+
start = j + 1
34+
end = start + length
35+
res.append(s[start:end])
36+
i = end
37+
return res

โ€Žgroup-anagrams/kangdaia.pyโ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
3+
"""
4+
๋‹จ์–ด ๋ชฉ๋ก์—์„œ ์• ๋„ˆ๊ทธ๋žจ๋ผ๋ฆฌ ๊ทธ๋ฃนํ™”ํ•ด์„œ, ๊ทธ๋ฃนํ™”๋œ ๋‹จ์–ด ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜.
5+
6+
N์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜, M์€ ๋‹จ์–ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋กœ ๊ฐ€์ •ํ•  ๋•Œ,
7+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(N * M log M)
8+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(N * M)
9+
10+
Args:
11+
strs (list[str]): ๋‹จ์–ด ๋ชฉ๋ก
12+
13+
Returns:
14+
list[list[str]]: ์• ๋„ˆ๊ทธ๋žจ ๊ทธ๋ฃนํ™”๋œ ๋‹จ์–ด ๋ชฉ๋ก
15+
"""
16+
seen = dict()
17+
for st in strs:
18+
st_key = "".join(sorted(st))
19+
if st_key in seen:
20+
seen[st_key].append(st)
21+
else:
22+
seen[st_key] = [st]
23+
return list(seen.values())
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Node:
2+
"""
3+
Trie ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋…ธ๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค.
4+
5+
key: ๋…ธ๋“œ์˜ ํ‚ค (๋ฌธ์ž)
6+
data: ๋…ธ๋“œ์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ (๋‹จ์–ด)
7+
children: ์ž์‹ ๋…ธ๋“œ๋“ค์„ ์ €์žฅํ•จ (ํ‚ค: ๋ฌธ์ž, ๊ฐ’: Node ๊ฐ์ฒด)
8+
"""
9+
def __init__(self, key, data=None):
10+
self.key = key
11+
self.data = data
12+
self.children = {}
13+
14+
15+
class Trie:
16+
"""
17+
Trie ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค.
18+
19+
head: Trie์˜ ๋ฃจํŠธ ๋…ธ๋“œ
20+
21+
insert(word): ๋‹จ์–ด๋ฅผ Trie์— ์‚ฝ์ž…ํ•˜๋Š” ๋ฉ”์„œ๋“œ
22+
search(word): Trie์—์„œ ๋‹จ์–ด๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ
23+
startsWith(prefix): Trie์—์„œ ์ ‘๋‘์‚ฌ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ
24+
25+
- ์‹œ๊ฐ„ ๋ณต์žก๋„: O(M) (M์€ ๋‹จ์–ด์˜ ๊ธธ์ด)
26+
- ๊ณต๊ฐ„ ๋ณต์žก๋„: O(N * M) (N์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜, M์€ ๋‹จ์–ด์˜ ์ตœ๋Œ€ ๊ธธ์ด)
27+
"""
28+
def __init__(self):
29+
self.head = Node(None)
30+
31+
def insert(self, word: str) -> None:
32+
curr = self.head
33+
for char in word:
34+
if char not in curr.children:
35+
curr.children[char] = Node(char)
36+
curr = curr.children[char]
37+
curr.data = word
38+
39+
def search(self, word: str) -> bool:
40+
curr = self.head
41+
for char in word:
42+
if char not in curr.children:
43+
return False
44+
curr = curr.children[char]
45+
return True if curr.data == word else False
46+
47+
def startsWith(self, prefix: str) -> bool:
48+
curr = self.head
49+
for char in prefix:
50+
if char not in curr.children:
51+
return False
52+
curr = curr.children[char]
53+
return True
54+
55+
56+
# Your Trie object will be instantiated and called as such:
57+
# obj = Trie()
58+
# obj.insert(word)
59+
# param_2 = obj.search(word)
60+
# param_3 = obj.startsWith(prefix)

โ€Žword-break/kangdaia.pyโ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: list[str]) -> bool:
3+
"""
4+
์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด s๋ฅผ ๋‹จ์–ด ์‚ฌ์ „ wordDict์˜ ๋‹จ์–ด๋“ค๋กœ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ํ•จ์ˆ˜.
5+
N์€ ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด, M์€ ๋‹จ์–ด ์‚ฌ์ „์˜ ์ตœ๋Œ€ ๋‹จ์–ด ๊ธธ์ด๋กœ ๊ฐ€์ •ํ•  ๋•Œ,
6+
์‹œ๊ฐ„ ๋ณต์žก๋„: O(N * M)
7+
๊ณต๊ฐ„ ๋ณต์žก๋„: O(N)
8+
9+
Args:
10+
s (str): ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด
11+
wordDict (list[str]): ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด ์‚ฌ์ „
12+
13+
Returns:
14+
bool: ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ๋‹จ์–ด ์‚ฌ์ „์˜ ๋‹จ์–ด๋“ค๋กœ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€
15+
"""
16+
word_check = [False] * (len(s) + 1)
17+
max_len = len(max(wordDict, key=len))
18+
word_check[0] = True
19+
for i in range(len(s)):
20+
if not word_check[i]:
21+
continue
22+
for j in range(i + 1, min(len(s), i + max_len) + 1):
23+
# i๊นŒ์ง€์˜ ๋‹จ์–ด๊ฐ€ ์‚ฌ์ „์— ์žˆ๊ณ , i๋ถ€ํ„ฐ j๊นŒ์ง€์˜ ๋‹จ์–ด๊ฐ€ ์‚ฌ์ „์— ์žˆ์œผ๋ฉด ๋ชจ๋“  ๋‹จ์–ด๊ฐ€ ์‚ฌ์ „์— ์žˆ๋Š” ์ƒํƒœ๋กœ ํŒ๋‹จ
24+
if word_check[i] and s[i:j] in wordDict:
25+
word_check[j] = True
26+
return word_check[-1]

0 commit comments

Comments
ย (0)