Skip to content

[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

Merged
merged 3 commits into from
May 2, 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
16 changes: 16 additions & 0 deletions best-time-to-buy-and-sell-stock/i-mprovising.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 조건문을 통해서 min_price를 설정해주었는데, min을 통해 더 깔끔하게 풀이할 수 있다는 것 배워갑니다.


return max_profit
19 changes: 19 additions & 0 deletions group-anagrams/i-mprovising.py
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))
Copy link
Contributor

Choose a reason for hiding this comment

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

sorted() 정렬이 포함되어 있는데 시간 복잡도를 O(n)으로 적어주셔서, 이 부분이 의아하여 찾아보니 해설 에서도 O(wlog(w)) 라고 적혀있었습니다.

파이썬에서 정렬의 성능이 O(n)까지 나오는지 궁금합니다 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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())
39 changes: 39 additions & 0 deletions implement-trie-prefix-tree/i-mprovising.py
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
27 changes: 27 additions & 0 deletions word-break/i-mprovising.py
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]