From 6d1ee5a3330b4ece452d15c2b62c5f500575a19b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sun, 11 Aug 2024 23:25:15 +0900 Subject: [PATCH 1/7] [LC] feat: contains-duplicate --- contains-duplicate/hajunyoo.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 contains-duplicate/hajunyoo.py diff --git a/contains-duplicate/hajunyoo.py b/contains-duplicate/hajunyoo.py new file mode 100644 index 000000000..ded649695 --- /dev/null +++ b/contains-duplicate/hajunyoo.py @@ -0,0 +1,6 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + string_len = len(nums) + set_len = len(set(nums)) + + return string_len != set_len \ No newline at end of file From a2d162185faedec2cf3627731899d47299ea28be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Wed, 14 Aug 2024 02:01:33 +0900 Subject: [PATCH 2/7] [LC] feat: number-of-bits --- number-of-1-bits/hajunyoo.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 number-of-1-bits/hajunyoo.py diff --git a/number-of-1-bits/hajunyoo.py b/number-of-1-bits/hajunyoo.py new file mode 100644 index 000000000..7609a495a --- /dev/null +++ b/number-of-1-bits/hajunyoo.py @@ -0,0 +1,9 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + n = int(n) + cnt = 0 + while n > 0: + if n % 2 == 1: + cnt += 1 + n = n // 2 + return cnt \ No newline at end of file From ca0a2b411e67bf56e1b24ff8098ef558e28f98c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Thu, 15 Aug 2024 23:29:45 +0900 Subject: [PATCH 3/7] [LC] feat: top k frequent elements --- top-k-frequent-elements/hajunyoo.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 top-k-frequent-elements/hajunyoo.py diff --git a/top-k-frequent-elements/hajunyoo.py b/top-k-frequent-elements/hajunyoo.py new file mode 100644 index 000000000..b432bb1ca --- /dev/null +++ b/top-k-frequent-elements/hajunyoo.py @@ -0,0 +1,19 @@ +from collections import defaultdict +from typing import List + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + counter_dict = defaultdict(int) + + for n in nums: + counter_dict[n] += 1 + + count_list = [] + for key, val in counter_dict.items(): + count_list.append((key, val)) + + count_list.sort(key=lambda x: x[1], reverse=True) + answer = [a for a, b in count_list[:k]] + + return answer \ No newline at end of file From 833fda5cb9fda26f2b996587e675fb08d0bd0593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Fri, 16 Aug 2024 00:22:13 +0900 Subject: [PATCH 4/7] [LC] feat: top k frequent elements (bst) --- kth-smallest-element-in-a-bst/hajunyoo.py | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/hajunyoo.py diff --git a/kth-smallest-element-in-a-bst/hajunyoo.py b/kth-smallest-element-in-a-bst/hajunyoo.py new file mode 100644 index 000000000..f2d512831 --- /dev/null +++ b/kth-smallest-element-in-a-bst/hajunyoo.py @@ -0,0 +1,30 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +from collections import defaultdict + + +class Solution: + def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: + + self.count = 0 + self.result = 0 + + def dfs(node): + if not node: + return + + dfs(node.left) + + self.count += 1 + if self.count == k: + self.result = node.val + return + + dfs(node.right) + + dfs(root) + return self.result \ No newline at end of file From 6139c9c3d1cec00466333052e315344a6b7cba96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sat, 17 Aug 2024 15:33:29 +0900 Subject: [PATCH 5/7] [LC] feat: palindromic-substring --- palindromic-substrings/hajunyoo.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 palindromic-substrings/hajunyoo.py diff --git a/palindromic-substrings/hajunyoo.py b/palindromic-substrings/hajunyoo.py new file mode 100644 index 000000000..87569cd34 --- /dev/null +++ b/palindromic-substrings/hajunyoo.py @@ -0,0 +1,18 @@ +class Solution: + def countSubstrings(self, s: str) -> int: + self.count = 0 + n = len(s) + + def two_pointer_expand(left, right): + while left >= 0 and right < n and s[left] == s[right]: + self.count += 1 + left -= 1 + right += 1 + + for i in range(0, n): + # 1, 3, 5 ... + two_pointer_expand(i, i) + # 2, 4, 6 ... + two_pointer_expand(i, i + 1) + + return self.count \ No newline at end of file From 0c8f19768a1654657561d8a274ae03cdd40368a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sat, 17 Aug 2024 22:13:10 +0900 Subject: [PATCH 6/7] [LC] 240817 add time complexity --- contains-duplicate/hajunyoo.py | 1 + kth-smallest-element-in-a-bst/hajunyoo.py | 1 + number-of-1-bits/hajunyoo.py | 1 + palindromic-substrings/hajunyoo.py | 1 + top-k-frequent-elements/hajunyoo.py | 1 + 5 files changed, 5 insertions(+) diff --git a/contains-duplicate/hajunyoo.py b/contains-duplicate/hajunyoo.py index ded649695..207e5ff75 100644 --- a/contains-duplicate/hajunyoo.py +++ b/contains-duplicate/hajunyoo.py @@ -1,4 +1,5 @@ class Solution: + # Time complexity: O(n) def containsDuplicate(self, nums: List[int]) -> bool: string_len = len(nums) set_len = len(set(nums)) diff --git a/kth-smallest-element-in-a-bst/hajunyoo.py b/kth-smallest-element-in-a-bst/hajunyoo.py index f2d512831..518e64a03 100644 --- a/kth-smallest-element-in-a-bst/hajunyoo.py +++ b/kth-smallest-element-in-a-bst/hajunyoo.py @@ -8,6 +8,7 @@ class Solution: + # Time complexity: O(n) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: self.count = 0 diff --git a/number-of-1-bits/hajunyoo.py b/number-of-1-bits/hajunyoo.py index 7609a495a..86c450dcd 100644 --- a/number-of-1-bits/hajunyoo.py +++ b/number-of-1-bits/hajunyoo.py @@ -1,4 +1,5 @@ class Solution: + # Time complexity: O(log n) def hammingWeight(self, n: int) -> int: n = int(n) cnt = 0 diff --git a/palindromic-substrings/hajunyoo.py b/palindromic-substrings/hajunyoo.py index 87569cd34..7ab18dec9 100644 --- a/palindromic-substrings/hajunyoo.py +++ b/palindromic-substrings/hajunyoo.py @@ -1,4 +1,5 @@ class Solution: + # Time complexity: O(n^2) = O(n) * O(n) def countSubstrings(self, s: str) -> int: self.count = 0 n = len(s) diff --git a/top-k-frequent-elements/hajunyoo.py b/top-k-frequent-elements/hajunyoo.py index b432bb1ca..7b986a3d7 100644 --- a/top-k-frequent-elements/hajunyoo.py +++ b/top-k-frequent-elements/hajunyoo.py @@ -3,6 +3,7 @@ class Solution: + # Time complexity: O(nlogn) -> O(n) + O(nlogn) + O(k) def topKFrequent(self, nums: List[int], k: int) -> List[int]: counter_dict = defaultdict(int) From b9ae75028048721f89475d7807359361200db615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9C=A0=ED=95=98=EC=A4=80?= Date: Sun, 18 Aug 2024 10:14:52 +0900 Subject: [PATCH 7/7] [LC] 240818 fix line break lint --- contains-duplicate/hajunyoo.py | 2 +- kth-smallest-element-in-a-bst/hajunyoo.py | 3 +-- number-of-1-bits/hajunyoo.py | 3 +-- palindromic-substrings/hajunyoo.py | 2 +- top-k-frequent-elements/hajunyoo.py | 2 +- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/contains-duplicate/hajunyoo.py b/contains-duplicate/hajunyoo.py index 207e5ff75..058172a4b 100644 --- a/contains-duplicate/hajunyoo.py +++ b/contains-duplicate/hajunyoo.py @@ -4,4 +4,4 @@ def containsDuplicate(self, nums: List[int]) -> bool: string_len = len(nums) set_len = len(set(nums)) - return string_len != set_len \ No newline at end of file + return string_len != set_len diff --git a/kth-smallest-element-in-a-bst/hajunyoo.py b/kth-smallest-element-in-a-bst/hajunyoo.py index 518e64a03..3deb0e4a6 100644 --- a/kth-smallest-element-in-a-bst/hajunyoo.py +++ b/kth-smallest-element-in-a-bst/hajunyoo.py @@ -10,7 +10,6 @@ class Solution: # Time complexity: O(n) def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: - self.count = 0 self.result = 0 @@ -28,4 +27,4 @@ def dfs(node): dfs(node.right) dfs(root) - return self.result \ No newline at end of file + return self.result diff --git a/number-of-1-bits/hajunyoo.py b/number-of-1-bits/hajunyoo.py index 86c450dcd..b8e0e07fc 100644 --- a/number-of-1-bits/hajunyoo.py +++ b/number-of-1-bits/hajunyoo.py @@ -1,10 +1,9 @@ class Solution: # Time complexity: O(log n) def hammingWeight(self, n: int) -> int: - n = int(n) cnt = 0 while n > 0: if n % 2 == 1: cnt += 1 n = n // 2 - return cnt \ No newline at end of file + return cnt diff --git a/palindromic-substrings/hajunyoo.py b/palindromic-substrings/hajunyoo.py index 7ab18dec9..08982c61b 100644 --- a/palindromic-substrings/hajunyoo.py +++ b/palindromic-substrings/hajunyoo.py @@ -16,4 +16,4 @@ def two_pointer_expand(left, right): # 2, 4, 6 ... two_pointer_expand(i, i + 1) - return self.count \ No newline at end of file + return self.count diff --git a/top-k-frequent-elements/hajunyoo.py b/top-k-frequent-elements/hajunyoo.py index 7b986a3d7..a26351c77 100644 --- a/top-k-frequent-elements/hajunyoo.py +++ b/top-k-frequent-elements/hajunyoo.py @@ -17,4 +17,4 @@ def topKFrequent(self, nums: List[int], k: int) -> List[int]: count_list.sort(key=lambda x: x[1], reverse=True) answer = [a for a, b in count_list[:k]] - return answer \ No newline at end of file + return answer