Skip to content

Commit

Permalink
Merge pull request #320 from mangodm-web/main
Browse files Browse the repository at this point in the history
[DM] Week 01 Solutions
  • Loading branch information
mangodm-web authored Aug 18, 2024
2 parents 0748714 + d96b82f commit 8ed4e81
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contains-duplicate/dm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import List


class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
6 changes: 6 additions & 0 deletions number-of-1-bits/dm.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions palindromic-substrings/dm.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions top-k-frequent-elements/dm.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8ed4e81

Please sign in to comment.