From a3b23afd74969752eb485224e17b406bac5e3cce Mon Sep 17 00:00:00 2001 From: coloryourlife <95chris0319@gmail.com> Date: Mon, 12 Aug 2024 19:12:50 -0700 Subject: [PATCH 1/6] solve: contains-duplicate --- contains-duplicate/Raft.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 contains-duplicate/Raft.py diff --git a/contains-duplicate/Raft.py b/contains-duplicate/Raft.py new file mode 100644 index 000000000..830219e9d --- /dev/null +++ b/contains-duplicate/Raft.py @@ -0,0 +1,10 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + appeared = defaultdict(int) + for n in nums: + if appeared[n] > 0: + return True + appeared[n] += 1 + + return False + From 19151dbd7d985c20591b923d1a97cc34822a7d88 Mon Sep 17 00:00:00 2001 From: coloryourlife <95chris0319@gmail.com> Date: Fri, 16 Aug 2024 22:16:18 -0700 Subject: [PATCH 2/6] solve: top-k-frequent-elements --- top-k-frequent-elements/raft.py | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 top-k-frequent-elements/raft.py diff --git a/top-k-frequent-elements/raft.py b/top-k-frequent-elements/raft.py new file mode 100644 index 000000000..a92f75ece --- /dev/null +++ b/top-k-frequent-elements/raft.py @@ -0,0 +1,44 @@ +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + # Frequency + freq = defaultdict(int) + for n in nums: + freq[n] += 1 + + freq_list = [] + for key, val in freq.items(): + freq_list.append((-1 * val, key)) + + heapq.heapify(freq_list) + + res = [] + for _ in range(k): + res.append(heapq.heappop(freq_list)[1]) + + return res + + # T: nlogn + # S: n + + def topKFrequent2(self, nums: List[int], k: int) -> List[int]: + res = [] + # Frequency + count = defaultdict(int) + for n in nums: + count[n] += 1 + + # Convert to frequency + frequency = [[] for _ in range(len(nums) + 1)] + for key, freq in count.items(): + frequency[freq].append(key) + + # top K + for freq in range(len(nums), 0, -1): + for n in frequency[freq]: + res.append(n) + + if len(res) == k: + return res + # T: n + # S: n + From 703cbe0761d2f9ebb9d3bba0b5dde20d1b0780d8 Mon Sep 17 00:00:00 2001 From: coloryourlife <95chris0319@gmail.com> Date: Fri, 16 Aug 2024 22:19:37 -0700 Subject: [PATCH 3/6] solve: number-of-1-bits --- number-of-1-bits/Raft.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 number-of-1-bits/Raft.py diff --git a/number-of-1-bits/Raft.py b/number-of-1-bits/Raft.py new file mode 100644 index 000000000..48f74fa8c --- /dev/null +++ b/number-of-1-bits/Raft.py @@ -0,0 +1,10 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + res = 0 + while n: + res += n % 2 + n = n >> 1 + return res +# T: logn +# S: 1 + From f77037424c984ae5849a2a9cb548dbf5f0d21cc2 Mon Sep 17 00:00:00 2001 From: coloryourlife <95chris0319@gmail.com> Date: Fri, 16 Aug 2024 22:22:03 -0700 Subject: [PATCH 4/6] solve: kth-smallest-element-in-a-bst --- kth-smallest-element-in-a-bst/Raft.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 kth-smallest-element-in-a-bst/Raft.py diff --git a/kth-smallest-element-in-a-bst/Raft.py b/kth-smallest-element-in-a-bst/Raft.py new file mode 100644 index 000000000..5b21826f2 --- /dev/null +++ b/kth-smallest-element-in-a-bst/Raft.py @@ -0,0 +1,16 @@ +class Solution: + def kthSmallest(self, root: Optional[TreeNode], k: int) -> int: + stack = [] + + while True: + while root: + stack.append(root) + root = root.left + root = stack.pop() + k -= 1 + if not k: + return root.val + root = root.right +# T: O(n) +# S: O(n) + From 052d1122d550f57ec99a7f5f1f6b3a71dfd6a25b Mon Sep 17 00:00:00 2001 From: coloryourlife <95chris0319@gmail.com> Date: Fri, 16 Aug 2024 22:26:40 -0700 Subject: [PATCH 5/6] solve: palindromic-substrings --- palindromic-substrings/Raft.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 palindromic-substrings/Raft.py diff --git a/palindromic-substrings/Raft.py b/palindromic-substrings/Raft.py new file mode 100644 index 000000000..904f9075f --- /dev/null +++ b/palindromic-substrings/Raft.py @@ -0,0 +1,18 @@ +class Solution: + def countSubstrings(self, s: str) -> int: + count = 0 + for i in range(len(s)): + count += self.countPalindrome(s, i, i) + count += self.countPalindrome(s, i, i + 1) + return count + + def countPalindrome(self, s, l, r): + count = 0 + while r < len(s) and l >= 0 and s[l] == s[r]: + count += 1 + l -= 1 + r += 1 + return count +# T: O(n^2) +# S: O(n^2) + From f10b34d236d21576d5507544a9a1bd6d4b3544a8 Mon Sep 17 00:00:00 2001 From: coloryourlife <95chris0319@gmail.com> Date: Fri, 16 Aug 2024 22:28:18 -0700 Subject: [PATCH 6/6] update: time, space complextity --- contains-duplicate/Raft.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contains-duplicate/Raft.py b/contains-duplicate/Raft.py index 830219e9d..bd5f51287 100644 --- a/contains-duplicate/Raft.py +++ b/contains-duplicate/Raft.py @@ -7,4 +7,6 @@ def containsDuplicate(self, nums: List[int]) -> bool: appeared[n] += 1 return False +# T: O(n) +# S: O(n)