From 5b0fc61a0b93fc4996ac3db30d12beb425344942 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Sun, 11 Aug 2024 22:03:39 +0900 Subject: [PATCH 1/7] [LC] contains-duplicate --- contains-duplicate/bemelon.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 contains-duplicate/bemelon.py diff --git a/contains-duplicate/bemelon.py b/contains-duplicate/bemelon.py new file mode 100644 index 000000000..c86c3c749 --- /dev/null +++ b/contains-duplicate/bemelon.py @@ -0,0 +1,10 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + # Time Complexity: O(n) + # Space Complexity: O(n) + seen = set() + for num in nums: + if num in seen: + return True + seen.add(num) + return False \ No newline at end of file From 5639aa5b06513cf16ea31e5209c96cdc19cc1184 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Sun, 11 Aug 2024 22:04:17 +0900 Subject: [PATCH 2/7] style: add empty line --- contains-duplicate/bemelon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/bemelon.py b/contains-duplicate/bemelon.py index c86c3c749..ee855039d 100644 --- a/contains-duplicate/bemelon.py +++ b/contains-duplicate/bemelon.py @@ -7,4 +7,4 @@ def containsDuplicate(self, nums: List[int]) -> bool: if num in seen: return True seen.add(num) - return False \ No newline at end of file + return False From 0bc526281431014e63b1ec5881e1aef00357af76 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Sun, 11 Aug 2024 23:19:17 +0900 Subject: [PATCH 3/7] Solve LC > numbers-of-1-bits --- number-of-1-bits/bemelon.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 number-of-1-bits/bemelon.py diff --git a/number-of-1-bits/bemelon.py b/number-of-1-bits/bemelon.py new file mode 100644 index 000000000..649fb5e32 --- /dev/null +++ b/number-of-1-bits/bemelon.py @@ -0,0 +1,18 @@ +class Solution: + def hammingWeight_v1(self, n: int) -> int: + # Recursive solution + # Time complexity: O(log n) + # Space complexity: O(log n) + if n == 0: return 0 + elif n % 2 == 0: return self.hammingWeight(n // 2) + else: return self.hammingWeight(n // 2) + 1 + + def hammingWeight(self, n: int) -> int: + # Iterative solution + # Time complexity: O(log n) + # Space complexity: O(1) + set_bit_cnt = 0 + while n > 0: + set_bit_cnt += n & 1 + n >>= 1 + return set_bit_cnt \ No newline at end of file From 2af581d66caae28bf7a6e23da896bd669d3304e6 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Sun, 11 Aug 2024 23:19:44 +0900 Subject: [PATCH 4/7] style: add empty line --- number-of-1-bits/bemelon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/number-of-1-bits/bemelon.py b/number-of-1-bits/bemelon.py index 649fb5e32..f41233514 100644 --- a/number-of-1-bits/bemelon.py +++ b/number-of-1-bits/bemelon.py @@ -15,4 +15,4 @@ def hammingWeight(self, n: int) -> int: while n > 0: set_bit_cnt += n & 1 n >>= 1 - return set_bit_cnt \ No newline at end of file + return set_bit_cnt From 4b0908be6f55a0fd82d72204104931a88347ab04 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Sun, 11 Aug 2024 23:29:04 +0900 Subject: [PATCH 5/7] Solve LC > top-k-frequent-elements --- top-k-frequent-elements/bemelon.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 top-k-frequent-elements/bemelon.py diff --git a/top-k-frequent-elements/bemelon.py b/top-k-frequent-elements/bemelon.py new file mode 100644 index 000000000..cef8f7104 --- /dev/null +++ b/top-k-frequent-elements/bemelon.py @@ -0,0 +1,22 @@ +from collections import defaultdict + +class Solution: + def topKFrequent(self, nums: list[int], k: int) -> list[int]: + """ + Time complexity: O(nlogn) + Space complexity: O(n) + """ + if len(nums) == k: + return list(set(nums)) + + num_cnt = defaultdict(int) + for num in nums: + num_cnt[num] += 1 + + return list( + sorted( + num_cnt, + key=lambda x: num_cnt[x], + reverse=True + ) + )[:k] From dd0d0b416fd189d83e1b8229ff28893c64d24ede Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Sun, 11 Aug 2024 23:45:34 +0900 Subject: [PATCH 6/7] Solve LC > kth-smallest-element-in-a-bst --- kth-smallest-element-in-a-bst/bemelon.py | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/bemelon.py diff --git a/kth-smallest-element-in-a-bst/bemelon.py b/kth-smallest-element-in-a-bst/bemelon.py new file mode 100644 index 000000000..809e1e0df --- /dev/null +++ b/kth-smallest-element-in-a-bst/bemelon.py @@ -0,0 +1,26 @@ +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + """ + Time Complexity: O(n) + Space Complexity: O(k) + """ + + def inOrder(self, root: TreeNode, asc_sorted_list: list[int], k: int) -> None: + if root.left: + self.inOrder(root.left, asc_sorted_list, k) + + asc_sorted_list.append(root.val) + + if len(asc_sorted_list) < k and root.right: + self.inOrder(root.right, asc_sorted_list, k) + + def kthSmallest(self, root: TreeNode, k: int) -> int: + asc_sorted_list = [] + self.inOrder(root, asc_sorted_list, k) + + return asc_sorted_list[k - 1] From 4341deb7ac91c4fd55b4c8daf5aa9f0bbe3d7d44 Mon Sep 17 00:00:00 2001 From: Hwang Kyu Do Date: Mon, 12 Aug 2024 00:27:11 +0900 Subject: [PATCH 7/7] Solve LC > palindromic substrings --- palindromic-substrings/bemelon.py | 49 +++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 palindromic-substrings/bemelon.py diff --git a/palindromic-substrings/bemelon.py b/palindromic-substrings/bemelon.py new file mode 100644 index 000000000..9bb9a39ab --- /dev/null +++ b/palindromic-substrings/bemelon.py @@ -0,0 +1,49 @@ +class Solution: + def countSubstrings_v1(self, s: str) -> int: + def isPalindrome(self, substr: str) -> bool: + return len(substr) <= 1 or (substr[0] == substr[-1] and self.isPalindrome(substr[1:-1])) + + # Brute-Force Solution - TLE + count = 0 + for l in range(1, len(s) + 1): + for start in range(0, len(s)): + if start + l > len(s): continue + + substr = s[start: start + l] + if (self.isPalindrome(substr)): + count += 1 + return count + + def countSubstrings(self, s: str) -> int: + """ + Dynamic Programming Solution + Time Complexity: O(N^2) + Space Complexity: O(N^2) + """ + n = len(s) + + # isPalindrome[i][j] => Palindrome at s[i:j]? + isPalindrome = [[False] * n for _ in range(n)] + answer = 0 + # 1. "a", "b", "c" are all Palindrome + for i in range(n): + isPalindrome[i][i] = True + answer += 1 + + # 2. "a{x}" are Palindrome if a == {x} + for i in range(n - 1): + if s[i] == s[i + 1]: + isPalindrome[i][i + 1] = True + answer += 1 + + # 3. else) str[i:j] is Palindrome if str[i + 1: j - 1] ... is Palinedrome + for size in range(3, n + 1): + for start in range(n - size + 1): + end = start + size - 1 + + if s[start] == s[end] and isPalindrome[start + 1][end - 1]: + isPalindrome[start][end] = True + answer += 1 + + return answer +