Skip to content

Commit

Permalink
Merge pull request #862 from Chaedie/main
Browse files Browse the repository at this point in the history
[Chaedie] Week 5
  • Loading branch information
Chaedie authored Jan 10, 2025
2 parents 777fd88 + 0843344 commit e9bff47
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
40 changes: 40 additions & 0 deletions best-time-to-buy-and-sell-stock/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Solution: 1) 2์ค‘ ํฌ๋ฌธ์„ ๋Œ๋ฉด์„œ max๊ฐ’์„ ๊ตฌํ•œ๋‹ค.
Time: O(n^2)
Space: O(1)
Time Limit Exceeded
"""


class Solution:
def maxProfit(self, prices: List[int]) -> int:
result = 0
for l in range(len(prices) - 1):
for r in range(l + 1, len(prices)):
result = max(result, prices[r] - prices[l])

return result


"""
Solution:
1) prices๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ max_profit ์„ ์ฐพ๋Š”๋‹ค.
2) profit ์€ current price - min_profit๋กœ ๊ตฌํ•œ๋‹ค.
Time: O(n)
Space: O(1)
"""


class Solution:
def maxProfit(self, prices: List[int]) -> int:
n = len(prices)
max_profit = 0
min_price = prices[0]

for i in range(1, len(prices)):
profit = prices[i] - min_price
max_profit = max(max_profit, profit)
min_price = min(prices[i], min_price)
return max_profit
22 changes: 22 additions & 0 deletions group-anagrams/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Solution:
1) hash map ์— sorted_word๋ฅผ ํ‚ค๋กœ, ํ•ด๋‹น sorted_word ์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ๋“ค์„ ๋ฐธ๋ฅ˜๋กœ ๋„ฃ์Šต๋‹ˆ๋‹ค.
Time: O(n^2 logn)= O(n) * O(nlogn)
Space: O(n)
"""


class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagram_words = defaultdict(list)
# dict = {sorted_word: [word, word]}

for i in range(len(strs)):
word = strs[i]
sorted_word = "".join(sorted(word))
anagram_words[sorted_word].append(word)

result = []
for arr in anagram_words.values():
result.append(arr)
return result
59 changes: 59 additions & 0 deletions implement-trie-prefix-tree/Chaedie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
Solution:
Trie ๊ตฌ์กฐ์— ๋Œ€ํ•ด ๋ฐฐ์›Œ๋ณด์•˜์Šต๋‹ˆ๋‹ค.
TrieNode ๋Š” children๊ณผ ending์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ์Šต๋‹ˆ๋‹ค.
1) insert:
1.1) ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๋ฅผ Trie ๊ตฌ์กฐ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
1.2) ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์—ด์—์„  ending ์„ True ๋กœ setํ•ฉ๋‹ˆ๋‹ค.
2) search:
2.1) ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž๊ฐ€ node.children ์— ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด False ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
2.2) ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊ฐ€ ending ์ธ์ง€ ํŒ๋ณ„ํ•ฉ๋‹ˆ๋‹ค.
3) startsWith
3.1) search ์™€ ๋™์ผํ•˜๊ฒŒ ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•ฉ๋‹ˆ๋‹ค.
3.2) ending ์—ฌ๋ถ€์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ True ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
"""


class TrieNode:
def __init__(self):
self.children = {}
self.ending = False


class Trie:
def __init__(self):
self.root = TrieNode()

def insert(self, word: str) -> None:
node = self.root

for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.ending = True

def search(self, word: str) -> bool:
node = self.root

for char in word:
if char not in node.children:
return False
node = node.children[char]
return node.ending

def startsWith(self, prefix: str) -> bool:
node = self.root

for char in prefix:
if char not in node.children:
return False
node = node.children[char]
return True


# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)

0 comments on commit e9bff47

Please sign in to comment.