From eba954e845162f054db718e38cda2f99e986db01 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Mon, 2 Sep 2024 20:12:31 +0900 Subject: [PATCH 1/7] Valid Palindrome Solution --- valid-palindrome/kayden.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-palindrome/kayden.py diff --git a/valid-palindrome/kayden.py b/valid-palindrome/kayden.py new file mode 100644 index 000000000..35f1040cf --- /dev/null +++ b/valid-palindrome/kayden.py @@ -0,0 +1,25 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class Solution: + + def isPalindrome(self, s: str) -> bool: + string = "" + + for letter in s: + if letter.isalnum(): + string += letter.lower() + + + def valid(s): + st, en = 0, len(s)-1 + + while st < en: + if s[st] != s[en]: + return False + + st += 1 + en -= 1 + + return True + + return valid(string) From 8c3ed237f541fa15d7966a435d3063eea22887d2 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Thu, 5 Sep 2024 23:48:13 +0900 Subject: [PATCH 2/7] Fix Valid Palindrome Solution --- valid-palindrome/kayden.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/valid-palindrome/kayden.py b/valid-palindrome/kayden.py index 35f1040cf..e41f48ade 100644 --- a/valid-palindrome/kayden.py +++ b/valid-palindrome/kayden.py @@ -6,19 +6,19 @@ def isPalindrome(self, s: str) -> bool: string = "" for letter in s: - if letter.isalnum(): + if letter.isalnum(): # if ('a' <= char <= 'z') or ('A' <= char <= 'Z') or ('0' <= char <= '9'): string += letter.lower() def valid(s): - st, en = 0, len(s)-1 + start, end = 0, len(s)-1 - while st < en: - if s[st] != s[en]: + while start < end: + if s[start] != s[end]: return False - st += 1 - en -= 1 + start += 1 + end -= 1 return True From 0fd659f313bc88db80e48606a75acaa9ef85601c Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 6 Sep 2024 00:00:16 +0900 Subject: [PATCH 3/7] Missing Number Solution --- missing-number/kayden.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 missing-number/kayden.py diff --git a/missing-number/kayden.py b/missing-number/kayden.py new file mode 100644 index 000000000..8a11721c6 --- /dev/null +++ b/missing-number/kayden.py @@ -0,0 +1,7 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(1) +class Solution: + def missingNumber(self, nums: List[int]) -> int: + n = len(nums) + total = n*(n+1)//2 + return total - sum(nums) From 6f3e43e92bf4be2358fa34392d32bfb00ede604f Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 6 Sep 2024 01:05:03 +0900 Subject: [PATCH 4/7] Longest Consecutive Sequence Solution --- longest-common-subsequence/kayden.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 longest-common-subsequence/kayden.py diff --git a/longest-common-subsequence/kayden.py b/longest-common-subsequence/kayden.py new file mode 100644 index 000000000..d43456d11 --- /dev/null +++ b/longest-common-subsequence/kayden.py @@ -0,0 +1,17 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(N) +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + nums = set(nums) + answer = 0 + + for num in nums: + if num - 1 not in nums: + length = 1 + + while num + length in nums: + length += 1 + + answer = max(answer, length) + + return answer From 19785ec7c848919ba943ed4d2b83450360b1e114 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 6 Sep 2024 01:05:17 +0900 Subject: [PATCH 5/7] Word Search Solution --- word-search/kayden.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 word-search/kayden.py diff --git a/word-search/kayden.py b/word-search/kayden.py new file mode 100644 index 000000000..f8b72137d --- /dev/null +++ b/word-search/kayden.py @@ -0,0 +1,38 @@ +# 시간복잡도: O(N*M*4^limit) limit: word의 길이 +# 공간복잡도: O(N) +class Solution: + def exist(self, board: List[List[str]], word: str) -> bool: + m = len(board) + n = len(board[0]) + limit = len(word) + visited = [[False for _ in range(n)] for _ in range(m)] + dx = [0, 0, -1, 1] + dy = [-1, 1, 0, 0] + + target = 0 + + def dfs(x, y, idx): + + if idx == limit - 1: + return True + + for i in range(4): + nx, ny = x + dx[i], y + dy[i] + + if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny] and board[nx][ny] == word[idx + 1]: + visited[nx][ny] = True + if dfs(nx, ny, idx + 1): + return True + visited[nx][ny] = False + + return False + + for i in range(m): + for j in range(n): + if board[i][j] == word[target]: + visited[i][j] = True + if dfs(i, j, 0): + return True + visited[i][j] = False + + return False From 31620b558ceac8e927688deb0ff0de93fe3eb381 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 6 Sep 2024 01:46:38 +0900 Subject: [PATCH 6/7] Maximum Product Subarray Solution --- maximum-product-subarray/kayden.py | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 maximum-product-subarray/kayden.py diff --git a/maximum-product-subarray/kayden.py b/maximum-product-subarray/kayden.py new file mode 100644 index 000000000..d5b66cc24 --- /dev/null +++ b/maximum-product-subarray/kayden.py @@ -0,0 +1,33 @@ +# 시간복잡도: O(N) +# 공간복잡도: O(1) +class Solution: + def maxProduct(self, nums: List[int]) -> int: + n = len(nums) + positive, negative = 0, 0 + + if nums[0] > 0: + positive = nums[0] + else: + negative = nums[0] + + answer = max(nums) + + for i in range(1, n): + if nums[i] >= 0: + positive *= nums[i] + negative *= nums[i] + + if positive == 0: + positive = nums[i] + + else: + temp = positive + positive = negative * nums[i] + negative = temp * nums[i] + + if negative == 0: + negative = nums[i] + + answer = max(answer, positive) + + return answer From 570eaebae1090541886e7c5541502f2f32df8432 Mon Sep 17 00:00:00 2001 From: jinbeom Date: Fri, 6 Sep 2024 17:28:05 +0900 Subject: [PATCH 7/7] Fix Word Search Solution --- word-search/kayden.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/word-search/kayden.py b/word-search/kayden.py index f8b72137d..4f342efc5 100644 --- a/word-search/kayden.py +++ b/word-search/kayden.py @@ -1,5 +1,5 @@ # 시간복잡도: O(N*M*4^limit) limit: word의 길이 -# 공간복잡도: O(N) +# 공간복잡도: O(N*M) class Solution: def exist(self, board: List[List[str]], word: str) -> bool: m = len(board)