Skip to content

[HodaeSsi] Week 5 #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 12, 2025
Merged
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
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
Comment on lines +4 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알고리즘이 간결하고 이쁘네요. 다만 가독성을 위해 indent는 4space를 추천드립니다.


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)
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = collections.defaultdict(list)
from collections import defaultdict
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagrams = defaultdict(list)

파이썬에서 보통은 이런 식으로 모듈을 import 하는데, 이러면 어떤 모듈을 사용하는 파일인지 알기 쉬워져서 가독성이 더 좋아질 것 같습니다

for word in strs:
anagrams[''.join(sorted(word))].append(word)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
anagrams[''.join(sorted(word))].append(word)
key = ''.join(sorted(word))
anagrams[key].append(word)

코딩 골프 느낌의 pythonic한 짧은 코드도 좋지만, 해당 임시 변수가 어떤 역할을 하는지 선언한 변수명으로 알려주면 좋을 것 같습니다.

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 이 문제는 위의 implement-trie-prefix-tree와 연관되어 있어서 이를 활용하여 풀어보시면 도움이 되실 것 같습니다

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]

Loading