Skip to content

Commit 1bfe1f3

Browse files
authored
Merge pull request #853 from KwonNayeon/main
[KwonNayeon] Week 5
2 parents 70a4ead + e8a5387 commit 1bfe1f3

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Constraints:
3+
1. 1 <= prices.length <= 10^5
4+
2. 0 <= prices[i] <= 10^4
5+
6+
Time Complexity: O(n)
7+
8+
Space Complexity: O(1)
9+
10+
풀이 방법:
11+
- 배열을 ν•œ 번 μˆœνšŒν•˜λ©΄μ„œ:
12+
1. ν˜„μž¬κΉŒμ§€μ˜ μ΅œμ†Œ 가격(min_price)을 계속 κ°±μ‹ 
13+
2. ν˜„μž¬ κ°€κ²©μ—μ„œ μ΅œμ†Œ 가격을 λΊ€ κ°’(ν˜„μž¬ κ°€λŠ₯ν•œ 이읡)κ³Ό κΈ°μ‘΄ μ΅œλŒ€ 이읡을 λΉ„κ΅ν•˜μ—¬ 더 큰 값을 μ €μž₯
14+
- 이 λ°©μ‹μœΌλ‘œ 각 μ‹œμ μ—μ„œ κ°€λŠ₯ν•œ μ΅œλŒ€ 이읡을 계산함
15+
16+
To Do:
17+
- λ‹€λ₯Έ μ ‘κ·Ό 방법 찾아보기 (Two Pointers, Dynamic Programming)
18+
"""
19+
20+
class Solution:
21+
def maxProfit(self, prices: List[int]) -> int:
22+
min_price = prices[0]
23+
max_profit = 0
24+
25+
for i in range(1, len(prices)):
26+
min_price = min(min_price, prices[i])
27+
max_profit = max(max_profit, prices[i] - min_price)
28+
29+
return max_profit
30+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Constraints:
3+
1. 0 <= strs.length <= 200
4+
2. 0 <= strs[i].length <= 200
5+
3. strs[i]λŠ” μœ λ‹ˆμ½”λ“œ λ¬Έμžλ“€λ‘œλ§Œ ꡬ성됨
6+
4. encode된 string은 λ°˜λ“œμ‹œ decode κ°€λŠ₯ν•΄μ•Ό 함
7+
8+
Time Complexity: O(n)
9+
10+
Space Complexity: O(n)
11+
12+
풀이방법:
13+
- encode: 각 λ¬Έμžμ—΄ 뒀에 '#' μΆ”κ°€ν•˜μ—¬ ν•˜λ‚˜λ‘œ ν•©μΉ¨
14+
- decode: '#'을 κΈ°μ€€μœΌλ‘œ λ¬Έμžμ—΄ λΆ„ν• 
15+
16+
Further consideration:
17+
- ν˜„μž¬ 방식은 μž…λ ₯ λ¬Έμžμ—΄μ— '#'이 ν¬ν•¨λœ 경우 문제 λ°œμƒ κ°€λŠ₯
18+
- κ°œμ„  방법: λ¬Έμžμ—΄ 길이 + κ΅¬λΆ„μž + λ¬Έμžμ—΄ ν˜•μ‹ μ‚¬μš©
19+
예: ["Hello", "World"] -> "5#Hello5#World"
20+
"""
21+
22+
class Solution:
23+
"""
24+
@param: strs: a list of strings
25+
@return: encodes a list of strings to a single string.
26+
Examples:
27+
input: ["Hello", "World"]
28+
output: "Hello#World"
29+
"""
30+
def encode(self, strs):
31+
result = ""
32+
for s in strs:
33+
result = result + s + "#"
34+
return result[:-1]
35+
36+
"""
37+
@param: str: A string
38+
@return: decodes a single string to a list of strings
39+
Examples:
40+
input: "Hello#World"
41+
output: ["Hello", "World"]
42+
"""
43+
def decode(self, str):
44+
return str.split("#")

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Constraints:
3+
1. 1 <= strs.length <= 10^4
4+
2. 0 <= strs[i].length <= 100
5+
3. strs[i] consists of lowercase English letters.
6+
7+
Time Complexity: O(N * K * log K)
8+
- N은 μž…λ ₯ λ¬Έμžμ—΄μ˜ 개수 (strs의 길이)
9+
- KλŠ” κ°€μž₯ κΈ΄ λ¬Έμžμ—΄μ˜ 길이
10+
- 각 λ¬Έμžμ—΄μ„ μ •λ ¬ν•˜λŠ”λ° K * log Kκ°€ ν•„μš”ν•˜κ³ 
11+
- 이걸 N개의 λ¬Έμžμ—΄μ— λŒ€ν•΄ μˆ˜ν–‰
12+
13+
Space Complexity: O(N * K)
14+
- N개의 λ¬Έμžμ—΄μ„ μ €μž₯ν•΄μ•Ό 함
15+
- 각 λ¬Έμžμ—΄μ˜ κΈΈμ΄λŠ” μ΅œλŒ€ K
16+
- anagram_dict에 λͺ¨λ“  λ¬Έμžμ—΄μ„ μ €μž₯ν•˜κΈ° λ•Œλ¬Έ
17+
"""
18+
19+
class Solution:
20+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
21+
anagram_dict = {}
22+
23+
for s in strs:
24+
sorted_str = ''.join(sorted(s))
25+
26+
if sorted_str in anagram_dict:
27+
anagram_dict[sorted_str].append(s)
28+
else:
29+
anagram_dict[sorted_str] = [s]
30+
31+
return list(anagram_dict.values())
32+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Constraints:
3+
1. 1 <= word.length, prefix.length <= 2000
4+
2. word and prefix consist only of lowercase English letters.
5+
3. At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.
6+
7+
Time Complexity:
8+
- insert: O(m), m은 μž…λ ₯ λ‹¨μ–΄μ˜ 길이
9+
- search: O(m), m은 검색 λ‹¨μ–΄μ˜ 길이
10+
- startsWith: O(m), m은 μ ‘λ‘μ‚¬μ˜ 길이
11+
12+
Space Complexity:
13+
- O(N*M), N은 μ €μž₯된 λ‹¨μ–΄μ˜ 개수, M은 평균 단어 길이
14+
15+
μ΄ν•΄ν•œ λ‚΄μš©:
16+
1. Prefix(접두사)의 의미
17+
- 문법적 접두사(un-, pre- λ“±)κ°€ μ•„λ‹˜
18+
- λ‹¨μˆœνžˆ λ‹¨μ–΄μ˜ μ•žλΆ€λΆ„μ— μžˆλŠ” λ¬Έμžμ—΄μ„ 의미
19+
- 예: "apple"의 접두사 -> "a", "ap", "app", "appl", "apple"
20+
21+
2. μ‹€ν–‰ 방식
22+
- μž…λ ₯으둜 μ£Όμ–΄μ§„ λͺ…λ Ήμ–΄λ₯Ό 순차적으둜 μ‹€ν–‰
23+
- 예: ["Trie", "insert", "search"] μˆœμ„œλŒ€λ‘œ μ‹€ν–‰
24+
- 각 λͺ…령어에 ν•΄λ‹Ήν•˜λŠ” μΈμžκ°’λ„ μˆœμ„œλŒ€λ‘œ 적용
25+
26+
3. 자료ꡬ쑰 선택 (Trie with Hash Table)
27+
- Trie의 각 λ…Έλ“œμ—μ„œ μžμ‹ λ…Έλ“œλ₯Ό μ €μž₯ν•  λ•Œ ν•΄μ‹œ ν…Œμ΄λΈ” μ‚¬μš©
28+
- ν‚€: λ‹€μŒ 문자, κ°’: ν•΄λ‹Ή 문자의 TrieNode
29+
- μž₯점: λ‹€μŒ 문자 검색이 O(1)둜 κ°€λŠ₯
30+
31+
Notes:
32+
- 혼자 κ΅¬ν˜„ν•˜κΈ° μ–΄λ €μ›Œμ„œ, κ³΅λΆ€ν•œ λ‚΄μš©μ„ μ •λ¦¬λ§Œ ν–ˆμŠ΅λ‹ˆλ‹€.
33+
"""
34+
35+
# 트리의 각 "λ…Έλ“œ(λ§ˆλ””)"λ₯Ό ν‘œν˜„ν•˜λŠ” 클래슀
36+
class TrieNode:
37+
def __init__(self):
38+
# children은 "λ‹€μŒ κΈ€μžλ“€μ„ μ €μž₯ν•˜λŠ” 곡간"
39+
# 예: 'a' λ‹€μŒμ— 'p'κ°€ 올 수 μžˆλ‹€λ©΄ children['p']에 μ €μž₯
40+
self.children = {}
41+
42+
# is_endλŠ” "μ—¬κΈ°κΉŒμ§€κ°€ ν•˜λ‚˜μ˜ 단어가 λ˜λŠ”μ§€"λ₯Ό ν‘œμ‹œ
43+
# 예: "app"을 μ €μž₯ν–ˆλ‹€λ©΄, pλ…Έλ“œμ˜ is_endκ°€ True
44+
self.is_end = False
45+
46+
# 트리 전체λ₯Ό κ΄€λ¦¬ν•˜λŠ” 클래슀
47+
class Trie:
48+
def __init__(self):
49+
# 트리의 μ‹œμž‘μ  생성
50+
# μ‹€μ œ κΈ€μžλ“€μ€ root λ°‘μ—μ„œλΆ€ν„° μ‹œμž‘
51+
self.root = TrieNode()
52+
53+
# μƒˆλ‘œμš΄ 단어λ₯Ό νŠΈλ¦¬μ— μΆ”κ°€ν•˜λŠ” ν•¨μˆ˜
54+
def insert(self, word: str) -> None:
55+
node = self.root
56+
for char in word:
57+
if char not in node.children:
58+
node.children[char] = TrieNode()
59+
node = node.children[char]
60+
node.is_end = True
61+
62+
# 단어가 νŠΈλ¦¬μ— μžˆλŠ”μ§€ μ°ΎλŠ” ν•¨μˆ˜
63+
def search(self, word: str) -> bool:
64+
node = self.root
65+
for char in word:
66+
if char in node.children:
67+
node = node.children[char]
68+
else:
69+
return False
70+
return node.is_end
71+
72+
# ν•΄λ‹Ή μ ‘λ‘μ‚¬λ‘œ μ‹œμž‘ν•˜λŠ” 단어가 μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” ν•¨μˆ˜
73+
def startsWith(self, prefix: str) -> bool:
74+
node = self.root
75+
for char in prefix:
76+
if char in node.children:
77+
node = node.children[char]
78+
else:
79+
return False
80+
return True
81+
82+
"""
83+
μ‚¬μš© μ˜ˆμ‹œ:
84+
trie = Trie()
85+
trie.insert("apple") # "apple" μ €μž₯
86+
trie.search("apple") # True λ°˜ν™˜ (apple이 있음)
87+
trie.search("app") # False λ°˜ν™˜ (app은 μ—†μŒ)
88+
trie.startsWith("app") # True λ°˜ν™˜ (app으둜 μ‹œμž‘ν•˜λŠ” 단어가 있음)
89+
"""

0 commit comments

Comments
Β (0)