Skip to content

Commit

Permalink
Merge pull request #860 from jungsiroo/main
Browse files Browse the repository at this point in the history
[jungsiroo] Week 5
  • Loading branch information
jungsiroo authored Jan 9, 2025
2 parents 7ae7503 + 3e2c6d9 commit d4b8b1f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
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

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 ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ๋‘์–ด ์ฒ˜๋ฆฌํ•˜๋ฉด ๊ฐ€๋…์„ฑ ๋†’๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
"""

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)
"""

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]

0 comments on commit d4b8b1f

Please sign in to comment.