Skip to content

Commit 2624cbe

Browse files
authored
Merge pull request #437 from KyrieJ95/main
[Week 4] Add KyrieJ95's solution for 3 / 5 problems
2 parents 8246eb1 + 6a7eccf commit 2624cbe

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

maximum-product-subarray/joon.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
# Time: O(n)
6+
# Space: O(n)
7+
def maxProduct(self, nums: List[int]) -> int:
8+
maxProducts = [nums[0]]
9+
minProducts = [nums[0]]
10+
# Store all max/minProducts[i]: the max/min product of all subarrays that have nums[i] as the last element.
11+
# Time: O(n)
12+
# Space: O(n)
13+
for num in nums[1:]:
14+
newMaxProduct = max(maxProducts[-1] * num, minProducts[-1] * num, num)
15+
newMinProduct = min(maxProducts[-1] * num, minProducts[-1] * num, num)
16+
maxProducts.append(newMaxProduct)
17+
minProducts.append(newMinProduct)
18+
return max(maxProducts)

missing-number/joon.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
# Time: O(n)
6+
# Space: O(1)
7+
def missingNumber(self, nums: List[int]) -> int:
8+
n = len(nums)
9+
# MissingNumber = (Sum of 1, 2, ..., n) - Sum of nums)
10+
# Time: O(n)
11+
return n * (n + 1) // 2 - sum(nums)

valid-palindrome/joon.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import re
2+
3+
4+
class Solution:
5+
# Time: O(n)
6+
# Space: O(n)
7+
def isPalindrome(self, s: str) -> bool:
8+
# 1. Convert string
9+
# Time: O(n)
10+
# Space: O(n) since re.sub() will internally use a new string of length n.
11+
s = re.sub('[^a-z0-9]', '', s.lower())
12+
length = len(s)
13+
# 2. Check if the string reads the same forward and backward, one by one.
14+
# Time: O(n)
15+
# Space: O(1)
16+
for i in range(length):
17+
if (s[i] != s[length - 1 - i]):
18+
return False
19+
return True

0 commit comments

Comments
 (0)