Skip to content

Commit 7165f94

Browse files
authored
Merge pull request DaleStudy#628 from dusunax/main
[SunaDu] Week 1
2 parents 9a149b9 + 055969d commit 7165f94

File tree

5 files changed

+205
-0
lines changed

5 files changed

+205
-0
lines changed

contains-duplicate/dusunax.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
# Leetcode 217. Contains Duplicate
3+
4+
use set to store distinct elements 🗂️
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(n)
10+
SC: O(n)
11+
```
12+
13+
### TC is O(n):
14+
- iterating through the list just once to convert it to a set.
15+
16+
### SC is O(n):
17+
- creating a set to store the distinct elements of the list.
18+
'''
19+
20+
class Solution:
21+
def containsDuplicate(self, nums: List[int]) -> bool:
22+
return len(nums) != len(set(nums))
23+

house-robber/dusunax.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
# Leetcode 198. House Robber
3+
4+
use **dynamic programming** to solve this problem. (bottom-up approach) 🧩
5+
6+
choose bottom-up approach for less space complexity.
7+
8+
## DP relation
9+
10+
```
11+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
12+
```
13+
14+
- **dp[i - 1]:** skip and take the value from the previous house
15+
- **dp[i - 2]:** rob the current house, add its value to the maximum money from two houses before
16+
17+
## Time and Space Complexity
18+
19+
```
20+
TC: O(n)
21+
SC: O(n)
22+
```
23+
24+
### TC is O(n):
25+
- iterating through the list just once to calculate the maximum money. = O(n)
26+
27+
### SC is O(n):
28+
- using a list to store the maximum money at each house. = O(n)
29+
30+
'''
31+
32+
class Solution:
33+
def rob(self, nums: List[int]) -> int:
34+
if len(nums) == 1:
35+
return nums[0]
36+
37+
dp = [0] * len(nums)
38+
dp[0] = nums[0]
39+
dp[1] = max(nums[0], nums[1])
40+
41+
for i in range(2, len(nums)):
42+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
43+
44+
return dp[-1]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'''
2+
# Leetcode 128. Longest Consecutive Sequence
3+
4+
keep time complexity O(n) by iterating through the set and accessing elements in O(1) time. ⚖️
5+
6+
## Time and Space Complexity
7+
```
8+
TC: O(n)
9+
SC: O(n)
10+
```
11+
12+
### TC is O(n):
13+
- iterating through the set. O(n)
14+
- accessing elements in the set. O(1)
15+
- while loop incrementing `current_num` while `current_num + 1 in nums_set`. O(1)
16+
17+
### SC is O(n):
18+
- creating a set from the list of numbers. O(n)
19+
'''
20+
21+
class Solution:
22+
def longestConsecutive(self, nums: List[int]) -> int:
23+
nums_set = set(nums) # O(n)
24+
longest_sequence = 0
25+
26+
for num in nums_set: # O(n)
27+
if (num - 1) not in nums_set:
28+
current_num = num
29+
current_sequence = 1
30+
31+
while current_num + 1 in nums_set: # O(1)
32+
current_num += 1
33+
current_sequence += 1
34+
35+
longest_sequence = max(current_sequence, longest_sequence)
36+
37+
return longest_sequence
38+

top-k-frequent-elements/dusunax.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
# Leetcode 347. Top K Frequent Elements
3+
4+
use **Counter** to count the frequency of each element in the list 🚀
5+
6+
- **solution 1**: use **sorted()** to sort the elements by their frequency in descending order.
7+
- **solution 2**: use **bucket sort** to sort the elements by their frequency in descending order. (efficient!)
8+
9+
## Time and Space Complexity
10+
11+
### solution 1: topKFrequent()
12+
13+
```
14+
TC: O(n log n)
15+
SC: O(n)
16+
```
17+
18+
#### TC is O(n log n):
19+
- iterating through the list just once to count the frequency of each element. = O(n)
20+
- sorting the elements by their frequency in descending order. = O(n log n)
21+
22+
#### SC is O(n):
23+
- using a Counter to store the frequency of each element. = O(n)
24+
- sorted() creates a new list that holds the elements of frequency_map. = O(n)
25+
- result list that holds the top k frequent elements. = O(k)
26+
27+
### solution 2: topKFrequentBucketSort()
28+
29+
```
30+
TC: O(n)
31+
SC: O(n)
32+
```
33+
34+
#### TC is O(n):
35+
- iterating through the list just once to count the frequency of each element. = O(n)
36+
- creating **buckets** to store the elements by their frequency. = O(n)
37+
- iterating through the buckets in reverse order to get only the top k frequent elements. = O(n)
38+
39+
#### SC is O(n):
40+
- using a Counter to store the frequency of each element. = O(n)
41+
- using buckets to store the elements by their frequency. = O(n)
42+
- result list that holds only the top k frequent elements. = O(k)
43+
'''
44+
45+
class Solution:
46+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
47+
frequency_map = Counter(nums) # TC: O(n), SC: O(n)
48+
sorted_frequencies = sorted(frequency_map.items(), key=lambda x: x[1], reverse=True) # TC: O(n log n), SC: O(n)
49+
50+
result = [] # SC: O(k)
51+
for e in sorted_frequencies: # TC: O(k)
52+
result.append(e[0])
53+
54+
return result[0:k]
55+
56+
def topKFrequentBucketSort(self, nums: List[int], k: int) -> List[int]:
57+
frequency_map = Counter(nums)
58+
n = len(nums)
59+
buckets = [[] for _ in range(n + 1)]
60+
61+
for num, freq in frequency_map.items():
62+
buckets[freq].append(num)
63+
64+
result = []
65+
for i in range(len(buckets) - 1, 0, -1):
66+
for num in buckets[i]:
67+
result.append(num)
68+
if len(result) == k:
69+
return result

valid-palindrome/dusunax.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
# Leetcode 125. Valid Palindrome
3+
4+
use `isalnum()` to filter out non-alphanumeric characters 🔍
5+
6+
## Time and Space Complexity
7+
8+
```
9+
TC: O(n)
10+
SC: O(n)
11+
```
12+
13+
### TC is O(n):
14+
- iterating through the string just once to filter out non-alphanumeric characters. O(n)
15+
16+
### SC is O(n):
17+
- `s.lower()` creates a new string. O(n)
18+
- creating a new string `converted_s` to store the filtered characters. O(n)
19+
- `converted_s[::-1]` creates a new reversed string. O(n)
20+
'''
21+
22+
class Solution:
23+
def isPalindrome(self, s: str) -> bool:
24+
if s == " ":
25+
return True
26+
27+
s = s.lower()
28+
converted_s = ''.join(c for c in s if c.isalnum())
29+
30+
return converted_s == converted_s[::-1]
31+

0 commit comments

Comments
 (0)