diff --git a/container-with-most-water/kayden.py b/container-with-most-water/kayden.py new file mode 100644 index 000000000..9bd0eb040 --- /dev/null +++ b/container-with-most-water/kayden.py @@ -0,0 +1,17 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class Solution: + def maxArea(self, height: List[int]) -> int: + l, r = 0, len(height) - 1 + answer = 0 + + while l < r: + + if height[l] < height[r]: + answer = max(answer, (r - l) * height[l]) + l += 1 + else: + answer = max(answer, (r - l) * height[r]) + r -= 1 + + return answer diff --git a/design-add-and-search-words-data-structure/kayden.py b/design-add-and-search-words-data-structure/kayden.py new file mode 100644 index 000000000..68784c31b --- /dev/null +++ b/design-add-and-search-words-data-structure/kayden.py @@ -0,0 +1,38 @@ +class Node: + def __init__(self, ending=False): + self.ending = ending + self.children = {} + + +class WordDictionary: + + def __init__(self): + self.head = Node(True) + + # 시간복잡도: O(W) + def addWord(self, word: str) -> None: + node = self.head + + for ch in word: + if ch not in node.children: + node.children.setdefault(ch, Node()) + node = node.children[ch] + + node.ending = True + + # 시간복잡도: O(W*N) W: word 길이 N: 자식 노드의 개수 + def search(self, word: str) -> bool: + def dfs(idx, node): + if idx == len(word): + return node.ending + + if word[idx] == '.': + for n in node.children.values(): + if dfs(idx + 1, n): + return True + elif word[idx] in node.children: + return dfs(idx + 1, node.children[word[idx]]) + else: + return False + + return dfs(0, self.head) diff --git a/longest-increasing-subsequence/kayden.py b/longest-increasing-subsequence/kayden.py new file mode 100644 index 000000000..d5eb84180 --- /dev/null +++ b/longest-increasing-subsequence/kayden.py @@ -0,0 +1,16 @@ +# 시간복잡도: O(NlogN) +# 공간복잡도: O(N) +from bisect import bisect_left + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + path = [] + + for num in nums: + idx = bisect_left(path, num) + if len(path) > idx: + path[idx] = num + else: + path.append(num) + + return len(path) diff --git a/spiral-matrix/kayden.py b/spiral-matrix/kayden.py new file mode 100644 index 000000000..4a7fe725a --- /dev/null +++ b/spiral-matrix/kayden.py @@ -0,0 +1,34 @@ +# 시간복잡도: O(N*M) +# 공간복잡도: O(N*M) +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + dx = [0, 1, 0, -1] + dy = [1, 0, -1, 0] + n = len(matrix[0]) + m = len(matrix) + visited = [[False] * n for _ in range(m)] + visited[0][0] = True + answer = [] + + + def dfs(x, y, direction): + answer.append(matrix[x][y]) + nx = x + dx[direction] + ny = y + dy[direction] + + + if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny]: + visited[nx][ny] = True + return dfs(nx, ny, direction) + + direction = (direction+1) % 4 + nx = x + dx[direction] + ny = y + dy[direction] + + if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny]: + visited[nx][ny] = True + return dfs(nx, ny, direction) + else: + return answer + + return dfs(0, 0, 0) diff --git a/valid-parentheses/kayden.py b/valid-parentheses/kayden.py new file mode 100644 index 000000000..680406ee5 --- /dev/null +++ b/valid-parentheses/kayden.py @@ -0,0 +1,19 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(1) +class Solution: + def isValid(self, s: str) -> bool: + if len(s) % 2 != 0: + return False + + stack = [] + matching_brackets = {'(': ')', '{': '}', '[': ']'} + + for char in s: + if char in matching_brackets: + stack.append(char) + elif stack and matching_brackets[stack[-1]] == char: + stack.pop() + else: + return False + + return not stack