Skip to content

[jungsiroo] Week 5 #860

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
Jan 9, 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
23 changes: 23 additions & 0 deletions best-time-to-buy-and-sell-stock/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
"""
가장 수익을 많이 얻을 수 있도록 저점에 매수, 고점에 매도
매수와 매도는 서로 다른 날

min_price를 빼가면서 price 업데이트

Time Complexity : O(n)
Space Complexity : O(1)
"""

min_price = max(prices)
days = len(prices)

for day in range(days):
min_price = min(prices[day], min_price)
prices[day] -= min_price
Copy link
Member

Choose a reason for hiding this comment

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

오, 입력 리스트를 변경하시군요. 신선한 접근입니다!


return max(prices)



44 changes: 44 additions & 0 deletions group-anagrams/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from collections import defaultdict

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
# Naive Solution : Sort string

# 각 단어마다 모두 정렬을 한 뒤 해당 값을 hash로 사용하여 딕셔너리에 추가하는 방법
# strs[i].length = k
# Time Complexity : O(n*klog(k))
# Space Complexity : O(n*k)

"""

n = len(strs)
word_dict = defaultdict(list)

for word in strs:
key = hash(''.join(sorted(word)))
word_dict[key].append(word)

ret = []
for value in word_dict.values():
ret.append(value)
return ret
"""

# Better Solution : Counting

# anagram 의 특성 중 알파벳 카운트 갯수가 같다는 것을 이용
# 카운트 갯수를 활용하여 key 값으로 처리
# Time Complexity : O(n*k)
# Space Complexity : O(n*k)
word_dict = defaultdict(list)

for word in strs:
freq = [0]*26
for char in word:
freq[ord(char) - ord('a')] += 1
word_dict[tuple(freq)].append(word)

return list(word_dict.values())



47 changes: 47 additions & 0 deletions implement-trie-prefix-tree/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
Recursive vs Iterative

재귀 방식의 경우, 만약 문자열의 길이가 굉장히 길다면 그만큼의 콜이 일어나고 이는 성능적으로 느려질 수 있음.
두 가지 모두 시간 복잡도 면에서는 O(m) 임 (m = len(string))

Node 클래스를 따로 두어 처리하면 가독성 높게 처리할 수 있다.
Copy link
Member

Choose a reason for hiding this comment

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

네, 덕분에 코드가 읽기 쉽네요 🙌

"""

class Node:
def __init__(self, key=None):
self.key = key
self.children = {}
self.is_end = False

class Trie:
def __init__(self):
self.head = Node()

def insert(self, word: str) -> None:
curr = self.head

for ch in word:
if ch not in curr.children:
curr.children[ch] = Node(ch)
curr = curr.children[ch]
curr.is_end = True

def search(self, word: str) -> bool:
curr = self.head

for ch in word:
if ch not in curr.children:
return False
curr = curr.children[ch]

return curr.is_end

def startsWith(self, prefix: str) -> bool:
curr = self.head

for ch in prefix:
if ch not in curr.children:
return False
curr = curr.children[ch]
return True

27 changes: 27 additions & 0 deletions word-break/jungsiroo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
# DP를 활용한 문제

"""
KMP나 Rabin Karp를 이용하여 푸는 문제인 줄 알았으나 전혀 아닌 문제
neetcode의 도움을 받았음

계속 순회를 하면서 if dp[j]를 통해 길이만큼 끊어주고 word_set에 있는지를 확인함
Time Complexity : O(n^2)
Space Complexity : O(n)
Comment on lines +10 to +11
Copy link
Member

Choose a reason for hiding this comment

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

알고리즘의 성능에 영향을 주는 입력 인자가 2개이니 복잡도 분석도 2개의 변수로 표현해야 하지 않을까요?

"""

word_set = set(wordDict)
n = len(s)

dp = [False] * (n + 1)
dp[0] = True

for i in range(1, n + 1):
for j in range(i):
if dp[j] and s[j:i] in word_set:
dp[i] = True
break

return dp[n]

Loading