diff --git a/best-time-to-buy-and-sell-stock/changhyumm.py b/best-time-to-buy-and-sell-stock/changhyumm.py new file mode 100644 index 0000000000..ed574fb68e --- /dev/null +++ b/best-time-to-buy-and-sell-stock/changhyumm.py @@ -0,0 +1,9 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + min_price = prices[0] + max_profit = 0 + for i in range(0, len(prices) - 1): + min_price = min(prices[i], min_price) + profit = prices[i+1] - min_price + max_profit = max(profit, max_profit) + return max_profit diff --git a/container-with-most-water/changhyumm.py b/container-with-most-water/changhyumm.py new file mode 100644 index 0000000000..99629993e2 --- /dev/null +++ b/container-with-most-water/changhyumm.py @@ -0,0 +1,23 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + # 시간복잡도 O(n2)으로 타임아웃 + # max_amount = 0 + # for i in range(len(height) - 1): + # for j in range(i + 1, len(height)): + # amount = (j-i) * min(height[i], height[j]) + # max_amount = max(amount, max_amount) + # return max_amount + + # 투포인터 활용 + # 시간복잡도 O(n) + max_amount = 0 + left = 0 + right = len(height) - 1 + while left < right: + amount = (right - left) * min(height[left], height[right]) + max_amount = max(amount, max_amount) + if height[left] < height[right]: + left += 1 + else: + right -= 1 + return max_amount diff --git a/design-add-and-search-words-data-structure/changhyumm.py b/design-add-and-search-words-data-structure/changhyumm.py new file mode 100644 index 0000000000..d7329c240b --- /dev/null +++ b/design-add-and-search-words-data-structure/changhyumm.py @@ -0,0 +1,40 @@ +# prefix 기반으로 탐색할 수 있는 트라이노드 자료구조 활용 +class TrieNode: + def __init__(self): + self.children = {} + self.is_ending = False + +class WordDictionary: + + def __init__(self): + self.root = TrieNode() + + def addWord(self, word: str) -> None: + current_node = self.root + for char in word: + if char not in current_node.children: + current_node.children[char] = TrieNode() + current_node = current_node.children[char] + current_node.is_ending = True + + def search(self, word): + def dfs(node, index): + # 단어자리수만큼 왔고, 끝단어인경우 찾기 성공 + if index == len(word): + return node.is_ending + # 와일드카드인 경우 자식들 전부 탐색 + if word[index] == ".": + for child in node.children.values(): + if dfs(child, index+1): + return True + # 일반적인 경우 다음 자식 탐색 + if word[index] in node.children: + return dfs(node.children[word[index]], index+1) + return False + return dfs(self.root, 0) + + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) \ No newline at end of file diff --git a/longest-increasing-subsequence/changhyumm.py b/longest-increasing-subsequence/changhyumm.py new file mode 100644 index 0000000000..5c5fc00e63 --- /dev/null +++ b/longest-increasing-subsequence/changhyumm.py @@ -0,0 +1,14 @@ +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + # dp[i] = nums[0]부터 nums[i]까지의 LIS 길이 + # 처음은 1 + n = len(nums) + dp = [1] * n + + for i in range(1, n): + for j in range(i): + if nums[j] < nums[i]: + # nums[i]가 더 큰 경우 길이 + 1 + dp[i] = max(dp[i], dp[j] + 1) + + return max(dp) # 전체 중 최대 \ No newline at end of file diff --git a/valid-parentheses/changhyumm.py b/valid-parentheses/changhyumm.py new file mode 100644 index 0000000000..dff39f8548 --- /dev/null +++ b/valid-parentheses/changhyumm.py @@ -0,0 +1,20 @@ +class Solution: + def isValid(self, s: str) -> bool: + brackets = {"(": ")", "{": "}", "[": "]"} + stack = [] + # 괄호의 특성상 stack 자료구조 활용 + # 시간 복잡도 O(n) + # 공간 복잡도 O(n) + for string in s: + if string not in brackets: + if len(stack) == 0: + return False + else: + target = stack.pop() + if string != target: + return False + else: + stack.append(brackets[string]) + if stack: + return False + return True