Skip to content

Commit 23e262a

Browse files
authored
Merge pull request DaleStudy#20 from JoyJaewon/week1-JoyJaewon
[Joy] Week1 Solutions
2 parents 2d6fd6c + 4d1fac0 commit 23e262a

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Brainstorm
3+
- Brute force: double for-loop O(N^2)
4+
- Efficient: single pass O(N)
5+
6+
Plan
7+
1. Initialize the variables
8+
2. Iterate through each price
9+
2-1. Update the min price
10+
2-2. Calculate and update max profit
11+
3. Return the max profit
12+
'''
13+
14+
15+
def maxProfit(prices):
16+
min_price = float('inf')
17+
max_profit = 0
18+
for price in prices:
19+
if price < min_price:
20+
min_price = price
21+
elif price - min_price > max_profit:
22+
max_profit = price - min_price
23+
return max_profit
24+
25+
#TC: O(N), SC: O(1)
26+
27+
# Normal Case
28+
print(maxProfit([7, 1, 5, 3, 6, 4]) == 5)
29+
print(maxProfit([7, 6, 4, 3, 1]) == 0)
30+
31+
# Edge Case
32+
print(maxProfit([]) == 0)
33+
print(maxProfit([1]) == 0)
34+

contains-duplicate/JoyJaewon.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Brainstrom
3+
- brute force: double for-loop (O^2)
4+
- efficient approach: use set O(N)
5+
6+
Plan
7+
1. Initialize the set
8+
2. Iterate over the array
9+
2-1. For each element, check if it's in the set. If it is, return True
10+
2-2. If not, add the num to the set
11+
3. Return False since no duplicates are found
12+
'''
13+
14+
def containsDuplicate(nums):
15+
num_set=set()
16+
for num in nums:
17+
if num in num_set:
18+
return True
19+
num_set.add(num)
20+
return False
21+
22+
#TC: O(N), SC: O(N)
23+
24+
#Normal case
25+
print(containsDuplicate([1, 2, 3, 1]) == True)
26+
print(containsDuplicate([1, 2, 3, 4]) == False)
27+
28+
#Edge case
29+
print(containsDuplicate([1]) == False)
30+
print(containsDuplicate([]) == False)
31+

two-sum/JoyJaewon.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Brainstorm
3+
- Brute force: double for-loop / recursion - O(N^2)
4+
- Two Pointer: O(nlogn) - sorting
5+
- Efficient approach: use hashmap to go through the array once O(N)
6+
7+
Plan
8+
1. Initialize the dictionary
9+
2. Iterate over the array
10+
2-1. For each number, calculate the complement
11+
2-2. check if the complement exists in the dictionary
12+
2-3. Return the indices if it exists
13+
2-4. If not, add the current number and its index to the dictionary
14+
'''
15+
16+
def twoSum(nums, target):
17+
memo = {}
18+
for i in range(len(nums)):
19+
diff = target - nums[i]
20+
if diff in memo:
21+
return [memo[diff], i]
22+
memo[nums[i]] = i
23+
24+
#TC:O(N), SC:O(N)
25+
26+
#Normal case
27+
print(twoSum([2, 7, 11, 15], 9) == [0, 1])
28+
print(twoSum([3, 2, 4], 6) == [1, 2])
29+
30+
#Edge case
31+
print(twoSum([1, 2], 3) == [0, 1])
32+
print(twoSum([1, 5, 10], 20) == None)
33+

valid-anagram/JoyJaewon.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Brainstorm
3+
- brute force: double for-loop O(N^2)
4+
- efficient: use hash map O(N)
5+
6+
plan
7+
1. check the string lengths. Return false if different
8+
2. Initialize the dictionary
9+
3. Check the dictionary
10+
3-1. If all counts are zero, strings are anagrams. Return True
11+
3-2. If not, return False
12+
'''
13+
def isAnagram(s, t):
14+
if len(s) != len(t):
15+
return False
16+
17+
count = {}
18+
for char in s:
19+
count[char] = count.get(char, 0) + 1
20+
21+
for char in t:
22+
if char in count:
23+
count[char] -= 1
24+
if count[char] < 0:
25+
return False
26+
else:
27+
return False
28+
29+
return all(x == 0 for x in count.values())
30+
31+
#TC: O(N), SC: O(N)
32+
33+
#Normal case
34+
print(isAnagram("anagram", "nagaram") == True)
35+
print(isAnagram("rat", "car") == False)
36+
37+
#Edge case
38+
print(isAnagram("", "") == True)
39+

valid-palindrome/JoyJaewon.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'''
2+
Brainstorm
3+
- Brute force: double for-loop O(N^2)
4+
- Efficient: two pointer O(N)
5+
6+
Plan
7+
1. Preprocess the input data (lowercase, remove non-alphanumeric char)
8+
2. Use two pointers to check for palindrome
9+
'''
10+
#Version 1
11+
def isPalindrome(s):
12+
filtered_chars = [c.lower() for c in s if c.isalnum()]
13+
left, right = 0, len(filtered_chars) - 1
14+
15+
while left < right:
16+
if filtered_chars[left] != filtered_chars[right]:
17+
return False
18+
left, right = left + 1, right - 1
19+
20+
return True
21+
22+
23+
#Version 2
24+
def isPalindrome2(s):
25+
filtered_chars = [c.lower() for c in s if c.isalnum()]
26+
return filtered_chars == filtered_chars[::-1]
27+
28+
29+
#TC: O(N), SC: O(N)
30+
31+
#Normal case
32+
print(isPalindrome("A man, a plan, a canal: Panama") == True)
33+
print(isPalindrome("race a car") == False)
34+
35+
#Edge case
36+
print(isPalindrome("") == True)
37+
print(isPalindrome(" ") == True)
38+
print(isPalindrome("a.") == True)
39+

0 commit comments

Comments
 (0)