-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
[HodaeSsi] Week 5 #883
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
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)) | ||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
파이썬에서 보통은 이런 식으로 모듈을 import 하는데, 이러면 어떤 모듈을 사용하는 파일인지 알기 쉬워져서 가독성이 더 좋아질 것 같습니다 |
||||||||||||||||||||||
for word in strs: | ||||||||||||||||||||||
anagrams[''.join(sorted(word))].append(word) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
코딩 골프 느낌의 pythonic한 짧은 코드도 좋지만, 해당 임시 변수가 어떤 역할을 하는지 선언한 변수명으로 알려주면 좋을 것 같습니다. |
||||||||||||||||||||||
return list(anagrams.values()) | ||||||||||||||||||||||
|
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 | ||
|
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
알고리즘이 간결하고 이쁘네요. 다만 가독성을 위해 indent는 4space를 추천드립니다.