-
-
Notifications
You must be signed in to change notification settings - Fork 195
[i-mprovising] Week 05 solution #1383
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,16 @@ | ||
""" | ||
Time complexity O(n) | ||
Space complexity O(1) | ||
|
||
Dynamic programming | ||
""" | ||
|
||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
max_profit = 0 | ||
min_price = prices[0] | ||
for p in prices: | ||
max_profit = max(max_profit, p - min_price) | ||
min_price = min(min_price, p) | ||
|
||
return max_profit |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
Time complexity O(n) | ||
--> O(n * wlog(w)) | ||
n : 주어지는 단어 개수 | ||
w : 평균 단어 길이 | ||
|
||
Space compexity O(n) | ||
|
||
hash table, sorting | ||
""" | ||
|
||
class Solution: | ||
def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
group = defaultdict(list) | ||
for s in strs: | ||
sorted_str = str(sorted(s)) | ||
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.
파이썬에서 정렬의 성능이 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. strs의 길이인 n을 기준으로 O(n)으로 작성을 했는데, 반복문 내에 단어를 정렬하는 것도 고려를 해야겠네요! 해당 부분 반영하여 시간 복잡도 수정했습니다. |
||
group[sorted_str].append(s) | ||
|
||
return list(group.values()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Time complexity O(n) | ||
""" | ||
|
||
class Node: | ||
def __init__(self, end=False): | ||
self.children = {} # hash table | ||
self.end = end | ||
|
||
class Trie: | ||
def __init__(self): | ||
self.root = Node(end=True) | ||
|
||
def insert(self, word: str) -> None: | ||
node = self.root | ||
|
||
for c in word: | ||
if c not in node.children: | ||
node.children[c] = Node() | ||
node = node.children[c] | ||
node.end = True | ||
|
||
def search(self, word: str) -> bool: | ||
node = self.root | ||
for c in word: | ||
if c not in node.children: | ||
return False | ||
node = node.children[c] | ||
if node.end: | ||
return True | ||
return False | ||
|
||
def startsWith(self, prefix: str) -> bool: | ||
node = self.root | ||
for c in prefix: | ||
if c not in node.children: | ||
return False | ||
node = node.children[c] | ||
return True |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
Time complexity O(n*m) n: len(s), m:len(wordDict) | ||
Space compexity O(n) | ||
|
||
dynamic programming | ||
""" | ||
|
||
class Solution: | ||
def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
dp = [False for _ in range(len(s))] | ||
for i in range(len(s)): | ||
flag = False | ||
for word in wordDict: | ||
n = len(word) | ||
if i - n + 1 < 0: | ||
continue | ||
if s[i-n+1:i+1] != word: | ||
continue | ||
if i - n + 1 == 0: | ||
flag = True | ||
break | ||
elif i - n + 1 > 0: | ||
if dp[i - n]: | ||
flag = True | ||
break | ||
dp[i] = flag | ||
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.
저는 조건문을 통해서
min_price
를 설정해주었는데,min
을 통해 더 깔끔하게 풀이할 수 있다는 것 배워갑니다.