diff --git a/container-with-most-water/HodaeSsi.py b/container-with-most-water/HodaeSsi.py new file mode 100644 index 000000000..bb43d265a --- /dev/null +++ b/container-with-most-water/HodaeSsi.py @@ -0,0 +1,17 @@ +# 시간 복잡도 : O(n) +# 공간 복잡도 : O(1) +# 문제 유형 : Two Pointer +class Solution: + def maxArea(self, height: List[int]) -> int: + left, right = 0, len(height) - 1 + max_area = 0 + + while left < right: + max_area = max(max_area, (right - left) * min(height[left], height[right])) + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return max_area + diff --git a/design-add-and-search-words-data-structure/HodaeSsi.py b/design-add-and-search-words-data-structure/HodaeSsi.py new file mode 100644 index 000000000..a75f077c7 --- /dev/null +++ b/design-add-and-search-words-data-structure/HodaeSsi.py @@ -0,0 +1,30 @@ +# 문제유형 : Trie +class WordDictionary: + def __init__(self): + self.trie = {} + + # 시간 복잡도 : O(N) + # 공간 복잡도 : O(N) + def addWord(self, word: str) -> None: + node = self.trie + for char in word: + if char not in node: + node[char] = {} + node = node[char] + node['$'] = True + + # 시간 복잡도 : O(m^N) (m : 철자의 종류, N : 문자열의 길이) + def search(self, word: str) -> bool: + return self.dfs(0, self.trie, word) + + def dfs(self, i, node, word): + if i == len(word): + return '$' in node + if word[i] == '.': + for child in node.values(): + if isinstance(child, dict) and self.dfs(i + 1, child, word): + return True + if word[i] in node: + return self.dfs(i + 1, node[word[i]], word) + return False + diff --git a/longest-increasing-subsequence/HodaeSsi.py b/longest-increasing-subsequence/HodaeSsi.py new file mode 100644 index 000000000..16aba4253 --- /dev/null +++ b/longest-increasing-subsequence/HodaeSsi.py @@ -0,0 +1,20 @@ +# 시간 복잡도 : O(nlogn) +# 공간 복잡도 : O(n) +# 문제 유형 : DP, Binary Search +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [] + for num in nums: + if not dp or num > dp[-1]: + dp.append(num) + else: + left, right = 0, len(dp) - 1 + while left < right: + mid = left + (right - left) // 2 + if dp[mid] < num: + left = mid + 1 + else: + right = mid + dp[right] = num + return len(dp) + diff --git a/spiral-matrix/HodaeSsi.py b/spiral-matrix/HodaeSsi.py new file mode 100644 index 000000000..5cd5e0cfc --- /dev/null +++ b/spiral-matrix/HodaeSsi.py @@ -0,0 +1,31 @@ +# 시간 복잡도 : O(m * n) +# 공간 복잡도 : O(1) +# 문제 유형 : 구현 +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + row, col = len(matrix), len(matrix[0]) + up, down, left, right = 0, row - 1, 0, col - 1 + result = [] + while True: + for i in range(left, right + 1): + result.append(matrix[up][i]) + up += 1 + if up > down: + break + for i in range(up, down + 1): + result.append(matrix[i][right]) + right -= 1 + if right < left: + break + for i in range(right, left - 1, -1): + result.append(matrix[down][i]) + down -= 1 + if down < up: + break + for i in range(down, up - 1, -1): + result.append(matrix[i][left]) + left += 1 + if left > right: + break + return result + diff --git a/valid-parentheses/HodaeSsi.py b/valid-parentheses/HodaeSsi.py new file mode 100644 index 000000000..b6302c716 --- /dev/null +++ b/valid-parentheses/HodaeSsi.py @@ -0,0 +1,16 @@ +# 시간 복잡도 : O(n) +# 공간 복잡도 : O(n) +# 문제 유형 : Stack +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + + for char in s: + if char in (')', '}', ']'): + if not stack or stack.pop() != {')': '(', '}': '{', ']': '['}[char]: + return False + else: + stack.append(char) + + return not stack +