From 480b9371239e2b45d2aef196f950d21e6638ca7b Mon Sep 17 00:00:00 2001 From: mangodm-web Date: Tue, 13 Aug 2024 23:04:34 +0900 Subject: [PATCH 1/4] feat: add contains duplicate solution --- contains-duplicate/dm.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 contains-duplicate/dm.py diff --git a/contains-duplicate/dm.py b/contains-duplicate/dm.py new file mode 100644 index 000000000..7f3a7d28e --- /dev/null +++ b/contains-duplicate/dm.py @@ -0,0 +1,6 @@ +from typing import List + + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(nums) != len(set(nums)) From ecb817ad9a694552057976858cef6b39d9e362ca Mon Sep 17 00:00:00 2001 From: mangodm-web Date: Tue, 13 Aug 2024 23:04:44 +0900 Subject: [PATCH 2/4] feat: add number of 1 bits solution --- number-of-1-bits/dm.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 number-of-1-bits/dm.py diff --git a/number-of-1-bits/dm.py b/number-of-1-bits/dm.py new file mode 100644 index 000000000..76cc40b47 --- /dev/null +++ b/number-of-1-bits/dm.py @@ -0,0 +1,6 @@ +class Solution: + def hammingWeight(self, n: int, acc: int = 0) -> int: + if n == 0: + return acc + + return self.hammingWeight(n // 2, acc + n % 2) From bae7af93fab4ebc1869111919b6d7986e6543bea Mon Sep 17 00:00:00 2001 From: mangodm-web Date: Tue, 13 Aug 2024 23:04:57 +0900 Subject: [PATCH 3/4] feat: add top k frequent elements solution --- top-k-frequent-elements/dm.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 top-k-frequent-elements/dm.py diff --git a/top-k-frequent-elements/dm.py b/top-k-frequent-elements/dm.py new file mode 100644 index 000000000..674a6b622 --- /dev/null +++ b/top-k-frequent-elements/dm.py @@ -0,0 +1,21 @@ +from typing import List + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + count_dict = {} + frequency_bucket = [[] for i in range(len(nums) + 1)] + result = [] + + for num in nums: + count_dict[num] = count_dict.get(num, 0) + 1 + + for num, count in count_dict.items(): + frequency_bucket[count].append(num) + + for i in range(len(frequency_bucket) - 1, 0, -1): + for num in frequency_bucket[i]: + result.append(num) + + if len(result) == k: + return result From d96b82f68f673360c64718005504c6659ee39cb8 Mon Sep 17 00:00:00 2001 From: mangodm-web Date: Thu, 15 Aug 2024 22:19:09 +0900 Subject: [PATCH 4/4] feat: add palindromic substrings solution --- palindromic-substrings/dm.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 palindromic-substrings/dm.py diff --git a/palindromic-substrings/dm.py b/palindromic-substrings/dm.py new file mode 100644 index 000000000..54d8f2656 --- /dev/null +++ b/palindromic-substrings/dm.py @@ -0,0 +1,23 @@ +class Solution: + def countPalindrome(self, s: str, left: int, right: int) -> int: + result = 0 + + while left >= 0 and right < len(s) and s[left] == s[right]: + result += 1 + left -= 1 + right += 1 + + return result + + def countSubstrings(self, s: str) -> int: + total_count = 0 + + for i in range(len(s)): + left = right = i + total_count += self.countPalindrome(s, left, right) + + for i in range(len(s) - 1): + left, right = i, i + 1 + total_count += self.countPalindrome(s, left, right) + + return total_count