-
-
Notifications
You must be signed in to change notification settings - Fork 195
[sejineer] Week 05 solutions #1398
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
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,15 @@ | ||
""" | ||
시간 복잡도: O(N) | ||
공간 복잡도: O(1) | ||
""" | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
buy = prices[0] | ||
result = 0 | ||
|
||
for price in prices: | ||
profit = price - buy | ||
buy = min(buy, price) | ||
result = max(profit, result) | ||
|
||
return result | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
시간 복잡도: O(N * K) N=문자열 개수, 평균 문자열 길이 = K (최대 100) | ||
공간 복잡도: O(N * K) | ||
""" | ||
from collections import Counter, defaultdict | ||
|
||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
strs_dict = defaultdict(list) | ||
for s in strs: | ||
s_counter = Counter(s) | ||
strs_dict[frozenset(s_counter.items())].append(s) | ||
|
||
return list(strs_dict.values()) | ||
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. 역시 파이썬은 제공해주는 메서드가 많아서 자바에 비해 코드가 간단한 것이 좋은 것 같습니다 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Trie: | ||
|
||
def __init__(self): | ||
self.root = {"$": True} | ||
|
||
def insert(self, word: str) -> None: | ||
node = self.root | ||
for ch in word: | ||
if ch not in node: | ||
node[ch] = {"$": False} | ||
node = node[ch] | ||
node["$"] = True | ||
|
||
|
||
def search(self, word: str) -> bool: | ||
node = self.root | ||
for ch in word: | ||
if ch not in node: | ||
return False | ||
node = node[ch] | ||
return node["$"] | ||
|
||
def startsWith(self, prefix: str) -> bool: | ||
node = self.root | ||
for ch in prefix: | ||
if ch not in node: | ||
return False | ||
node = node[ch] | ||
return True |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
""" | ||
시간 복잡도: O(n * D * L) n = 문자열 길이, D = 사전 크기, L = 단어 평균 길이 | ||
공간 복잡도: O(n) | ||
""" | ||
class Solution: | ||
def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
|
||
@cache | ||
def dfs(k: int) -> bool: | ||
if k == len(s): | ||
return True | ||
for word in wordDict: | ||
if s[k : k + len(word)] == word: | ||
if dfs(k + len(word)): | ||
return True | ||
return False | ||
|
||
return dfs(0) | ||
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. 저는 재귀로 풀었었는데 dfs로도 풀 수 있네요! 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. DP 풀이도 한 번 도전해 보겠습니다! |
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.
저는 max, min 메서드를 사용을 하지는 않고 풀었는데 이렇게 푸니 코드가 더 깔끔해보이네요!