Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions best-time-to-buy-and-sell-stock/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
max_profit = 0

for price in prices[1:]:
if price < min_price:
min_price = price
elif price - min_price > max_profit:
max_profit = price - min_price

return max_profit
40 changes: 40 additions & 0 deletions encode-and-decode-strings/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
# write your code here
result = ""
for word in strs:
word_size = len(word)
result += str(word_size) + "#" + word

return result
# input ["lint","code","love","you"]
# output "4#lint4#code4#love3#you"

"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
# write your code here
result = []
i = 0
while i < len(str):
j = i
while str[j] != "#": #find the position of '#'
j += 1

my_number = int(str[i:j])

start = j + 1 # add 1 to skip '#'
end = j + 1 + my_number

result.append(str[start:end])
i = end

return result
# input "4#lint4#code4#love3#you"
# output ["lint","code","love","you"]
10 changes: 10 additions & 0 deletions group-anagrams/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
group_anagram = defaultdict(list)
# 초기화: 키가 없으면 자동으로 list() 즉, []를 실행해서 값을 만듦

for word in strs:
sorted_word = ''.join(sorted(word))
group_anagram[sorted_word].append(word)

return list(group_anagram.values())
42 changes: 42 additions & 0 deletions implement-trie-prefix-tree/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Trie:

def __init__(self):
self.children = {}
self.is_end = False

def insert(self, word: str) -> None:
node = self

for c in word:
if c not in node.children:
node.children[c] = Trie()
node = node.children[c]

node.is_end = True


def search(self, word: str) -> bool:
node = self

for c in word:
if c not in node.children:
return False
node = node.children[c]

return node.is_end

def startsWith(self, prefix: str) -> bool:
node = self
for c in prefix:
if c not in node.children:
return False
node = node.children[c]
return True



# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
25 changes: 25 additions & 0 deletions word-break/daiyongg-kim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

""" Failed Attempt
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:

for word in wordDict:
if word in s:
s = s.replace(word, '')
else:
return False
return len(s) == 0
"""
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
word_set = set(wordDict)

dp = [False] * (len(s) + 1)
dp[0] = True

for i in range(1, len(s) + 1):
for j in range(i):
if dp[j] and s[j:i] in word_set:
dp[i] = True
break
return dp[len(s)]