Skip to content

[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

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

Choose a reason for hiding this comment

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

저는 max, min 메서드를 사용을 하지는 않고 풀었는데 이렇게 푸니 코드가 더 깔끔해보이네요!

14 changes: 14 additions & 0 deletions group-anagrams/sejineer.py
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())
Copy link
Contributor

Choose a reason for hiding this comment

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

역시 파이썬은 제공해주는 메서드가 많아서 자바에 비해 코드가 간단한 것이 좋은 것 같습니다

29 changes: 29 additions & 0 deletions implement-trie-prefix-tree/sejineer.py
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
18 changes: 18 additions & 0 deletions word-break/sejineer.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 재귀로 풀었었는데 dfs로도 풀 수 있네요!
다른 해설을 보았을 때 DP로도 가능하다고 해서, DP 방식도 도전해 보셨으면 좋겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DP 풀이도 한 번 도전해 보겠습니다!