Skip to content

Commit f0abdfd

Browse files
authored
Merge pull request #2153 from daiyongg-kim/main
[daiyongg-kim] WEEK 05 solutions
2 parents b10234a + 1c3a16b commit f0abdfd

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
min_price = prices[0]
4+
max_profit = 0
5+
6+
for price in prices[1:]:
7+
if price < min_price:
8+
min_price = price
9+
elif price - min_price > max_profit:
10+
max_profit = price - min_price
11+
12+
return max_profit
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution:
2+
"""
3+
@param: strs: a list of strings
4+
@return: encodes a list of strings to a single string.
5+
"""
6+
def encode(self, strs):
7+
# write your code here
8+
result = ""
9+
for word in strs:
10+
word_size = len(word)
11+
result += str(word_size) + "#" + word
12+
13+
return result
14+
# input ["lint","code","love","you"]
15+
# output "4#lint4#code4#love3#you"
16+
17+
"""
18+
@param: str: A string
19+
@return: decodes a single string to a list of strings
20+
"""
21+
def decode(self, str):
22+
# write your code here
23+
result = []
24+
i = 0
25+
while i < len(str):
26+
j = i
27+
while str[j] != "#": #find the position of '#'
28+
j += 1
29+
30+
my_number = int(str[i:j])
31+
32+
start = j + 1 # add 1 to skip '#'
33+
end = j + 1 + my_number
34+
35+
result.append(str[start:end])
36+
i = end
37+
38+
return result
39+
# input "4#lint4#code4#love3#you"
40+
# output ["lint","code","love","you"]

group-anagrams/daiyongg-kim.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
group_anagram = defaultdict(list)
4+
# 초기화: 키가 없으면 자동으로 list() 즉, []를 실행해서 값을 만듦
5+
6+
for word in strs:
7+
sorted_word = ''.join(sorted(word))
8+
group_anagram[sorted_word].append(word)
9+
10+
return list(group_anagram.values())
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
10+
for c in word:
11+
if c not in node.children:
12+
node.children[c] = Trie()
13+
node = node.children[c]
14+
15+
node.is_end = True
16+
17+
18+
def search(self, word: str) -> bool:
19+
node = self
20+
21+
for c in word:
22+
if c not in node.children:
23+
return False
24+
node = node.children[c]
25+
26+
return node.is_end
27+
28+
def startsWith(self, prefix: str) -> bool:
29+
node = self
30+
for c in prefix:
31+
if c not in node.children:
32+
return False
33+
node = node.children[c]
34+
return True
35+
36+
37+
38+
# Your Trie object will be instantiated and called as such:
39+
# obj = Trie()
40+
# obj.insert(word)
41+
# param_2 = obj.search(word)
42+
# param_3 = obj.startsWith(prefix)

word-break/daiyongg-kim.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
""" Failed Attempt
3+
class Solution:
4+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
5+
6+
for word in wordDict:
7+
if word in s:
8+
s = s.replace(word, '')
9+
else:
10+
return False
11+
return len(s) == 0
12+
"""
13+
class Solution:
14+
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
15+
word_set = set(wordDict)
16+
17+
dp = [False] * (len(s) + 1)
18+
dp[0] = True
19+
20+
for i in range(1, len(s) + 1):
21+
for j in range(i):
22+
if dp[j] and s[j:i] in word_set:
23+
dp[i] = True
24+
break
25+
return dp[len(s)]

0 commit comments

Comments
 (0)