Skip to content

Commit 4d1fac0

Browse files
committed
Add solutions for additional problems
1 parent 243d89f commit 4d1fac0

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ def containsDuplicate(nums):
2727

2828
#Edge case
2929
print(containsDuplicate([1]) == False)
30-
print(containsDuplicate([]) == False)
30+
print(containsDuplicate([]) == False)
31+

two-sum/JoyJaewon.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ def twoSum(nums, target):
2929

3030
#Edge case
3131
print(twoSum([1, 2], 3) == [0, 1])
32-
print(twoSum([1, 5, 10], 20) == None)
32+
print(twoSum([1, 5, 10], 20) == None)
33+

valid-anagram/JoyJaewon.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ def isAnagram(s, t):
1616

1717
count = {}
1818
for char in s:
19-
if char in count:
20-
count[char] += 1
21-
else:
22-
count[char] = 1
19+
count[char] = count.get(char, 0) + 1
2320

2421
for char in t:
2522
if char in count:
@@ -40,4 +37,3 @@ def isAnagram(s, t):
4037
#Edge case
4138
print(isAnagram("", "") == True)
4239

43-

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)