Skip to content

[Chaedie] Week 5 #862

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 4 commits into from
Jan 10, 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
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)
Loading