Skip to content

Commit 8d657e5

Browse files
authored
Merge pull request #2500 from gcount85/main
[gcount85] WEEK 05 Solutions
2 parents cc4a722 + 6adf92b commit 8d657e5

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
# Approach
3+
지금까지의 최저 가격을 갱신함과 동시에 최선의 이익도 업데이트합니다.
4+
5+
# Complexity
6+
- Time complexity: O(N)
7+
8+
- Space complexity: O(1)
9+
"""
10+
11+
12+
class Solution:
13+
def maxProfit(self, prices: list[int]) -> int:
14+
min_price = float("inf")
15+
answer = 0
16+
17+
for price in prices:
18+
min_price = min(min_price, price)
19+
answer = max(answer, price - min_price)
20+
21+
return answer

group-anagrams/gcount85.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
# Approach
3+
strs 배열을 순회하며 문자열을 정규화(정렬)하고,
4+
정규화 값이 같은 원소들끼리 모이도록 딕셔너리에 추가하여 최종 값을 반환한다.
5+
6+
# Complexity
7+
strs의 길이를 N, 문자열의 길이를 K라고 할 때,
8+
9+
- Time complexity: O(N*KlogK)
10+
- Space complexity: O(N*K)
11+
"""
12+
13+
from collections import defaultdict
14+
15+
16+
class Solution:
17+
def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
18+
anagram = defaultdict(list) # normalized str : str list
19+
for s in strs:
20+
anagram["".join(sorted(s))].append(s)
21+
return list(anagram.values())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class TrieNode:
2+
def __init__(self):
3+
self.children = {}
4+
self.is_end = False
5+
6+
7+
class Trie:
8+
9+
def __init__(self):
10+
self.root = TrieNode()
11+
12+
def insert(self, word: str) -> None:
13+
node = self.root
14+
for ch in word:
15+
if ch not in node.children:
16+
node.children[ch] = TrieNode()
17+
node = node.children[ch]
18+
node.is_end = True
19+
20+
def search(self, word: str) -> bool:
21+
node = self.root
22+
for ch in word:
23+
if ch not in node.children:
24+
return False
25+
node = node.children[ch]
26+
return node.is_end
27+
28+
def startsWith(self, prefix: str) -> bool:
29+
node = self.root
30+
for ch in prefix:
31+
if ch not in node.children:
32+
return False
33+
node = node.children[ch]
34+
return True
35+
36+
37+
# Your Trie object will be instantiated and called as such:
38+
# obj = Trie()
39+
# obj.insert(word)
40+
# param_2 = obj.search(word)
41+
# param_3 = obj.startsWith(prefix)

word-break/gcount85.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
# Intuition
3+
wordDict를 word들의 길이로 분류하고,
4+
s의 각 위치까지의 문자열을 완성할 수 있는지 dp 배열로 확인합니다.
5+
6+
# Complexity
7+
wordDict의 길이를 N, s의 길이를 K
8+
- Time complexity: O(N+K)
9+
- Space complexity: O(N+K)
10+
"""
11+
12+
from collections import defaultdict
13+
14+
15+
class Solution:
16+
def wordBreak(self, s: str, wordDict: list[str]) -> bool:
17+
n = len(s)
18+
dp = [False] * (n + 1)
19+
word_dict = defaultdict(set)
20+
for word in wordDict:
21+
word_dict[len(word)].add(word)
22+
23+
dp[0] = True
24+
for i in range(1, n + 1):
25+
for (
26+
k,
27+
v,
28+
) in word_dict.items():
29+
if i + k - 1 > n:
30+
continue
31+
if dp[i - 1] == False:
32+
continue
33+
if s[i - 1 : i + k - 1] in v:
34+
dp[i + k - 1] = True
35+
return dp[n]

0 commit comments

Comments
 (0)