Skip to content

Commit 04dccd2

Browse files
authored
Merge pull request #1 from unpo88/week-05-solutions
Week 05 solutions
2 parents 11f97b1 + 2093f87 commit 04dccd2

File tree

4 files changed

+276
-0
lines changed

4 files changed

+276
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
class Solution:
2+
def maxProfit(self, prices: list[int]) -> int:
3+
if not prices:
4+
return 0
5+
6+
min_price = prices[0]
7+
max_profit = 0
8+
9+
for price in prices[1:]:
10+
max_profit = max(max_profit, price - min_price)
11+
min_price = min(min_price, price)
12+
13+
return max_profit
14+
15+
16+
"""
17+
================================================================================
18+
풀이 κ³Όμ •
19+
================================================================================
20+
21+
[문제 이해]
22+
────────────────────────────────────────────────────────────────────────────────
23+
1. μ£Όμ–΄μ§„ λ°°μ—΄ pricesκ°€ 있고 prices[i]λŠ” iλ‚ μ˜ νŠΉμ • 주식 가격
24+
2. ν•˜λ‚˜μ˜ 주식을 사기 μœ„ν•΄ ν•˜λ£¨λ₯Ό μ„ νƒν•˜κ³ , κ·Έ 주식을 νŒ”κΈ° μœ„ν•΄ 미래의 λ‹€λ₯Έ 날을 선택
25+
3. 이 κ±°λž˜μ—μ„œ 얻을 수 μžˆλŠ” μ΅œλŒ€ 이읡을 λ°˜ν™˜
26+
4. 이읡을 얻을 수 μ—†λ‹€λ©΄ 0을 λ°˜ν™˜
27+
28+
μ˜ˆμ‹œ 1: [7, 1, 5, 3, 6, 4] β†’ 5
29+
2일에 μ‚¬μ„œ 5일에 νŒ”λ©΄ β†’ 6 - 1 = 5κ°€ λ‚˜μ˜€λ©΄μ„œ κ°€μž₯ 큰 κ°’
30+
31+
μ˜ˆμ‹œ 2: [7, 6, 4, 3, 1] β†’ 0
32+
μ•„λ¬΄λŸ° 이읡이 μžˆλŠ” 날이 μ—†μŒ
33+
34+
35+
[1μ°¨ μ‹œλ„] Brute Force μ ‘κ·Ό
36+
────────────────────────────────────────────────────────────────────────────────
37+
5. λͺ¨λ“  (ꡬ맀일, 판맀일) μŒμ„ λΉ„κ΅ν•΄μ„œ μ΅œλŒ€ 이읡 μ°ΎκΈ°
38+
6. 이쀑 반볡문으둜 κ΅¬ν˜„
39+
40+
profit = 0
41+
for i in range(len(prices)):
42+
for j in range(i+1, len(prices)):
43+
profit = max(profit, prices[j] - prices[i])
44+
return profit
45+
46+
7. μ‹œκ°„λ³΅μž‘λ„: O(nΒ²) β†’ μ‹œκ°„ 초과 λ°œμƒ!
47+
48+
49+
[2μ°¨ μ‹œλ„] μ΅œμ ν™” 방법 κ³ λ―Ό
50+
────────────────────────────────────────────────────────────────────────────────
51+
8. 그러면 λ‹€λ₯Έ 풀이 방식이 ν•„μš”ν•  것 같은데
52+
9. 쑰건을 정리:
53+
- 이전 값이 λ‹€μŒ 값보닀 크면 버렀도 λœλ‹€
54+
- 이전 값이 λ‹€μŒ 값보닀 μž‘μœΌλ©΄ ν•΄λ‹Ή 값을 κΈ°λ‘ν•œλ‹€
55+
- 이미 기둝된게 있으면 비ꡐ해본닀
56+
57+
[7, 1, 5, 9, 3, 6, 4]
58+
min을 μΆ”μ ν•˜λ©΄μ„œ ν˜„μž¬κ°€ - min 쀑 μ΅œλŒ€κ°’μ„ 찾으면 됨!
59+
60+
61+
[μ΅œμ’… κ΅¬ν˜„] Kadane's Algorithm λ³€ν˜•
62+
────────────────────────────────────────────────────────────────────────────────
63+
10. ν•œ 번의 순회둜 μ΅œμ†Œ ꡬ맀가와 μ΅œλŒ€ 이읡을 λ™μ‹œμ— 좔적
64+
11. 각 μ‹œμ μ—μ„œ:
65+
- ν˜„μž¬κ°€ - μ΅œμ†Œκ°€ = μ§€κΈˆ νŒ”λ©΄ μ–»λŠ” 이읡
66+
- μ΅œλŒ€ 이읡 κ°±μ‹ 
67+
- μ΅œμ†Œκ°€ κ°±μ‹ 
68+
69+
min_price = prices[0]
70+
max_profit = 0
71+
72+
for price in prices[1:]:
73+
max_profit = max(max_profit, price - min_price)
74+
min_price = min(min_price, price)
75+
76+
12. μ‹œκ°„λ³΅μž‘λ„: O(n) - ν•œ 번의 순회
77+
13. κ³΅κ°„λ³΅μž‘λ„: O(1) - μƒμˆ˜ κ³΅κ°„λ§Œ μ‚¬μš©
78+
"""

β€Žgroup-anagrams/unpo88.pyβ€Ž

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
anagrams = defaultdict(list)
4+
for s in strs:
5+
anagrams["".join(sorted(s))].append(s)
6+
7+
return list(anagrams.values())
8+
9+
10+
"""
11+
================================================================================
12+
풀이 κ³Όμ •
13+
================================================================================
14+
15+
[문제 이해]
16+
────────────────────────────────────────────────────────────────────────────────
17+
1. λ¬Έμžμ—΄ λ°°μ—΄ strsκ°€ μ£Όμ–΄μ§€λ©΄ μ• λ„ˆκ·Έλž¨λΌλ¦¬ κ·Έλ£Ήν™”ν•˜μ—¬ λ°˜ν™˜
18+
2. μ• λ„ˆκ·Έλž¨: 같은 λ¬Έμžλ“€μ„ μž¬λ°°μ—΄ν•˜μ—¬ λ§Œλ“  단어 (각 문자λ₯Ό ν•œ λ²ˆμ”©λ§Œ μ‚¬μš©)
19+
3. 결과의 μˆœμ„œλŠ” μƒκ΄€μ—†μŒ
20+
21+
μ˜ˆμ‹œ 1: ["eat", "tea", "tan", "ate", "nat", "bat"]
22+
β†’ [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]]
23+
24+
μ˜ˆμ‹œ 2: [""]
25+
β†’ [[""]]
26+
27+
μ˜ˆμ‹œ 3: ["a"]
28+
β†’ [["a"]]
29+
30+
31+
[μ ‘κ·Ό 방법] ν•΄μ‹œλ§΅ + μ •λ ¬ ν‚€
32+
────────────────────────────────────────────────────────────────────────────────
33+
4. μ• λ„ˆκ·Έλž¨μ˜ νŠΉμ§•: μ •λ ¬ν•˜λ©΄ 같은 λ¬Έμžμ—΄μ΄ 됨
34+
- "eat" β†’ "aet"
35+
- "tea" β†’ "aet"
36+
- "ate" β†’ "aet"
37+
38+
5. μ •λ ¬λœ λ¬Έμžμ—΄μ„ ν‚€λ‘œ μ‚¬μš©ν•˜λŠ” ν•΄μ‹œλ§΅ ꡬ성
39+
- ν‚€: μ •λ ¬λœ λ¬Έμžμ—΄
40+
- κ°’: ν•΄λ‹Ή μ• λ„ˆκ·Έλž¨ κ·Έλ£Ή 리슀트
41+
42+
6. κ΅¬ν˜„:
43+
anagrams = defaultdict(list)
44+
for s in strs:
45+
anagrams["".join(sorted(s))].append(s)
46+
return list(anagrams.values())
47+
48+
49+
[λ³΅μž‘λ„ 뢄석]
50+
────────────────────────────────────────────────────────────────────────────────
51+
7. μ‹œκ°„λ³΅μž‘λ„: O(n Γ— k log k)
52+
- n: λ¬Έμžμ—΄ 개수
53+
- k: κ°€μž₯ κΈ΄ λ¬Έμžμ—΄μ˜ 길이
54+
- 각 λ¬Έμžμ—΄μ„ μ •λ ¬ν•˜λŠ”λ° O(k log k)
55+
56+
8. κ³΅κ°„λ³΅μž‘λ„: O(n Γ— k)
57+
- ν•΄μ‹œλ§΅μ— λͺ¨λ“  λ¬Έμžμ—΄ μ €μž₯
58+
"""
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
class Trie:
2+
3+
def __init__(self):
4+
self.children = {}
5+
self.is_end = False
6+
7+
def insert(self, word: str) -> None:
8+
node = self
9+
for c in word:
10+
if c not in node.children:
11+
node.children[c] = Trie()
12+
node = node.children[c]
13+
node.is_end = True
14+
15+
def search(self, word: str) -> bool:
16+
node = self._find(word)
17+
return node is not None and node.is_end
18+
19+
def startsWith(self, prefix: str) -> bool:
20+
return self._find(prefix) is not None
21+
22+
def _find(self, s: str):
23+
node = self
24+
for c in s:
25+
if c not in node.children:
26+
return None
27+
node = node.children[c]
28+
return node
29+
30+
31+
"""
32+
================================================================================
33+
풀이 κ³Όμ •
34+
================================================================================
35+
36+
[문제 이해]
37+
────────────────────────────────────────────────────────────────────────────────
38+
1. Trie(트라이) 자료ꡬ쑰 κ΅¬ν˜„
39+
2. insert(word): 단어 μ‚½μž…
40+
3. search(word): μ •ν™•νžˆ μΌμΉ˜ν•˜λŠ” 단어 μžˆλŠ”μ§€
41+
4. startsWith(prefix): prefix둜 μ‹œμž‘ν•˜λŠ” 단어 μžˆλŠ”μ§€
42+
43+
44+
[1μ°¨ μ‹œλ„] ν•΄μ‹œλ§΅
45+
────────────────────────────────────────────────────────────────────────────────
46+
5. μ²˜μŒμ— ν•΄μ‹±μœΌλ‘œ ν’€μ—ˆλ”λ‹ˆ ν†΅κ³ΌλŠ” ν–ˆλŠ”λ° startsWithμ—μ„œ κ²°κ΅­ 전체 순회 ν•„μš”
47+
6. μ‹œκ°„λ³΅μž‘λ„ O(n Γ— m) β†’ λΉ„νš¨μœ¨μ , μ§„μ§œ Trie둜 ν’€μ–΄μ•Όκ² λ‹€
48+
49+
50+
[2μ°¨ μ‹œλ„] μ§„μ§œ Trie
51+
────────────────────────────────────────────────────────────────────────────────
52+
7. 각 λ…Έλ“œκ°€ children(μžμ‹ λ…Έλ“œλ“€)κ³Ό is_end(단어 끝 μ—¬λΆ€)λ₯Ό 가짐
53+
54+
8. ꡬ쑰 μ˜ˆμ‹œ: "app", "apple" μ‚½μž… μ‹œ
55+
56+
root
57+
β”‚
58+
a
59+
β”‚
60+
p
61+
β”‚
62+
p (is_end: "app")
63+
β”‚
64+
l
65+
β”‚
66+
e (is_end: "apple")
67+
68+
9. insert: 문자 ν•˜λ‚˜μ”© 따라가며 λ…Έλ“œ 생성, λ§ˆμ§€λ§‰μ— is_end = True
69+
10. search: 문자 따라간 ν›„ is_endκ°€ True인지 확인
70+
11. startsWith: 문자 λ”°λΌκ°ˆ 수 있으면 True
71+
72+
73+
[λ³΅μž‘λ„ 뢄석]
74+
────────────────────────────────────────────────────────────────────────────────
75+
12. μ‹œκ°„λ³΅μž‘λ„: O(m) - λͺ¨λ“  μ—°μ‚°
76+
- m: 단어/prefix 길이
77+
78+
13. κ³΅κ°„λ³΅μž‘λ„: O(n Γ— m)
79+
- n: 단어 개수, m: 평균 단어 길이
80+
"""

β€Žword-break/unpo88.pyβ€Ž

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution:
2+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
3+
word_set = set(wordDict)
4+
dp = [False] * (len(s) + 1)
5+
dp[0] = True
6+
7+
for i in range(1, len(s) + 1):
8+
for j in range(i):
9+
if dp[j] and s[j:i] in word_set:
10+
dp[i] = True
11+
break
12+
13+
return dp[len(s)]
14+
15+
16+
"""
17+
================================================================================
18+
풀이 κ³Όμ •
19+
================================================================================
20+
21+
[문제 이해]
22+
────────────────────────────────────────────────────────────────────────────────
23+
1. λ¬Έμžμ—΄ s와 사전 wordDictκ°€ 주어짐
24+
2. sλ₯Ό 사전에 μžˆλŠ” λ‹¨μ–΄λ“€λ‘œ λΆ„ν• ν•  수 μžˆλŠ”μ§€ νŒλ‹¨
25+
26+
μ˜ˆμ‹œ 1: s = "leetcode", wordDict = ["leet", "code"]
27+
β†’ True ("leet" + "code")
28+
29+
μ˜ˆμ‹œ 2: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
30+
β†’ False (μ–΄λ–»κ²Œ λ‚˜λˆ λ„ μ•ˆ 됨)
31+
32+
33+
[μ ‘κ·Ό 방법] DP
34+
────────────────────────────────────────────────────────────────────────────────
35+
3. dp[i] = s[0:i]λ₯Ό 사전 λ‹¨μ–΄λ“€λ‘œ λΆ„ν•  κ°€λŠ₯ν•œκ°€?
36+
37+
4. 점화식: dp[i] = dp[j] and s[j:i] in word_set (μ–΄λ–€ j에 λŒ€ν•΄)
38+
- dp[j]κ°€ True이고, s[j:i]κ°€ 사전에 있으면 dp[i] = True
39+
40+
5. μ˜ˆμ‹œ: s = "leetcode"
41+
42+
l e e t c o d e
43+
0 1 2 3 4 5 6 7 8
44+
T ? ? ? T ? ? ? T
45+
β”‚ β”‚ β”‚
46+
λΉˆλ¬Έμžμ—΄ "leet" "code"
47+
48+
dp[4] = dp[0] + "leet" β†’ True
49+
dp[8] = dp[4] + "code" β†’ True
50+
51+
52+
[λ³΅μž‘λ„ 뢄석]
53+
────────────────────────────────────────────────────────────────────────────────
54+
6. μ‹œκ°„λ³΅μž‘λ„: O(nΒ² Γ— m)
55+
- n: λ¬Έμžμ—΄ 길이
56+
- m: 평균 단어 길이 (ν•΄μ‹œ 비ꡐ)
57+
58+
7. κ³΅κ°„λ³΅μž‘λ„: O(n)
59+
- dp λ°°μ—΄ 크기
60+
"""

0 commit comments

Comments
Β (0)