-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from mangodm-web/main
[DM] Week 01 Solutions
- Loading branch information
Showing
4 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |