Skip to content

Commit

Permalink
Merge pull request #883 from HodaeSsi/main
Browse files Browse the repository at this point in the history
[HodaeSsi] Week 5
  • Loading branch information
TonyKim9401 authored Jan 12, 2025
2 parents fddf7c3 + 5de30ec commit e537a6e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
13 changes: 13 additions & 0 deletions best-time-to-buy-and-sell-stock/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 시간 복잡도 : O(n)
# 공간 복잡도 : O(1)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = float('inf')
max_profit = 0

for price in prices:
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)

return max_profit

22 changes: 22 additions & 0 deletions encode-and-decode-strings/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 시간복잡도: O(n)
# 공간복잡도: O(1)

class Solution:
"""
@param: strs: a list of strings
@return: encodes a list of strings to a single string.
"""
def encode(self, strs):
if not strs:
return ""
return chr(257).join(strs)

"""
@param: str: A string
@return: decodes a single string to a list of strings
"""
def decode(self, str):
if not str:
return []
return str.split(chr(257))

10 changes: 10 additions & 0 deletions group-anagrams/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# 시간복잡도 : O(n*mlogm) (n은 strs의 길이, m은 strs의 원소의 길이)
# 공간복잡도 : O(n)

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = collections.defaultdict(list)
for word in strs:
anagrams[''.join(sorted(word))].append(word)
return list(anagrams.values())

31 changes: 31 additions & 0 deletions implement-trie-prefix-tree/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 시간복잡도: O(n) (n은 문자열의 길이)
# 공간복잡도: O(n) (n은 문자열의 길이)
class Trie:
def __init__(self):
self.root = {}
self.end = False

def insert(self, word: str) -> None:
node = self.root
for char in word:
if char not in node:
node[char] = {}
node = node[char]
node[self.end] = True

def search(self, word: str) -> bool:
node = self.root
for char in word:
if char not in node:
return False
node = node[char]
return self.end in node

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

15 changes: 15 additions & 0 deletions word-break/HodaeSsi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 시간복잡도 : O(n^2)
# 공간복잡도 : O(n)
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
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 wordDict:
dp[i] = True
break

return dp[-1]

0 comments on commit e537a6e

Please sign in to comment.