From 43765149dd744ac89c43f65916100b99a56c38c6 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 20 Sep 2024 23:29:08 +0900 Subject: [PATCH 1/7] Valid Parentheses Solution --- valid-parentheses/kayden.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 valid-parentheses/kayden.py diff --git a/valid-parentheses/kayden.py b/valid-parentheses/kayden.py new file mode 100644 index 000000000..3e71f3223 --- /dev/null +++ b/valid-parentheses/kayden.py @@ -0,0 +1,19 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +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 From 35fe589c5b915a17f43a54f368d3cc2256d784df Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 20 Sep 2024 23:32:47 +0900 Subject: [PATCH 2/7] Container With Most Water Solution --- container-with-most-water/kayden.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 container-with-most-water/kayden.py 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 From 36259bf136f81772204f37430cf7cf542cbe788b Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 20 Sep 2024 23:38:05 +0900 Subject: [PATCH 3/7] Design Add and Search Words Data Structure Solution --- .../kayden.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 design-add-and-search-words-data-structure/kayden.py 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) From 6d5ba91230cd5ea01ad7e868f8627150c10ff05d Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 20 Sep 2024 23:40:00 +0900 Subject: [PATCH 4/7] Longest Increasing Subsequence Solution --- longest-increasing-subsequence/kayden.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 longest-increasing-subsequence/kayden.py diff --git a/longest-increasing-subsequence/kayden.py b/longest-increasing-subsequence/kayden.py new file mode 100644 index 000000000..4ae82d5dd --- /dev/null +++ b/longest-increasing-subsequence/kayden.py @@ -0,0 +1,16 @@ +# 시간복잡도: O(N) +# 공간복잡도: 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) \ No newline at end of file From ea9359f7a57f67d5e0b41cd8e9052a91a90e00c3 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 20 Sep 2024 23:41:45 +0900 Subject: [PATCH 5/7] Spiral Matrix Solution --- longest-increasing-subsequence/kayden.py | 2 +- spiral-matrix/kayden.py | 34 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 spiral-matrix/kayden.py diff --git a/longest-increasing-subsequence/kayden.py b/longest-increasing-subsequence/kayden.py index 4ae82d5dd..0212fad8b 100644 --- a/longest-increasing-subsequence/kayden.py +++ b/longest-increasing-subsequence/kayden.py @@ -13,4 +13,4 @@ def lengthOfLIS(self, nums: List[int]) -> int: else: path.append(num) - return len(path) \ No newline at end of file + 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) From 1733a8105c26b3a4e3bf6163d8288522d3995c1b Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 20 Sep 2024 23:45:00 +0900 Subject: [PATCH 6/7] Fix Longest Increasing Subsequence Solution --- longest-increasing-subsequence/kayden.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/longest-increasing-subsequence/kayden.py b/longest-increasing-subsequence/kayden.py index 0212fad8b..d5eb84180 100644 --- a/longest-increasing-subsequence/kayden.py +++ b/longest-increasing-subsequence/kayden.py @@ -1,4 +1,4 @@ -# 시간복잡도: O(N) +# 시간복잡도: O(NlogN) # 공간복잡도: O(N) from bisect import bisect_left From 97dfab3606a54fd67dd1c70e88e007cebbe72826 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Sun, 22 Sep 2024 00:07:04 +0900 Subject: [PATCH 7/7] Fix Container With Most Water Solution --- valid-parentheses/kayden.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/valid-parentheses/kayden.py b/valid-parentheses/kayden.py index 3e71f3223..680406ee5 100644 --- a/valid-parentheses/kayden.py +++ b/valid-parentheses/kayden.py @@ -1,5 +1,5 @@ # 시간복잡도: O(N) -# 공간복잡도: O(N) +# 공간복잡도: O(1) class Solution: def isValid(self, s: str) -> bool: if len(s) % 2 != 0: