Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions maximum-product-subarray/joon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import List


class Solution:
# Time: O(n)
# Space: O(n)
def maxProduct(self, nums: List[int]) -> int:
maxProducts = [nums[0]]
minProducts = [nums[0]]
# Store all max/minProducts[i]: the max/min product of all subarrays that have nums[i] as the last element.
# Time: O(n)
# Space: O(n)
for num in nums[1:]:
newMaxProduct = max(maxProducts[-1] * num, minProducts[-1] * num, num)
newMinProduct = min(maxProducts[-1] * num, minProducts[-1] * num, num)
maxProducts.append(newMaxProduct)
minProducts.append(newMinProduct)
return max(maxProducts)
11 changes: 11 additions & 0 deletions missing-number/joon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from typing import List


class Solution:
# Time: O(n)
# Space: O(1)
def missingNumber(self, nums: List[int]) -> int:
n = len(nums)
# MissingNumber = (Sum of 1, 2, ..., n) - Sum of nums)
# Time: O(n)
return n * (n + 1) // 2 - sum(nums)
19 changes: 19 additions & 0 deletions valid-palindrome/joon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import re


class Solution:
# Time: O(n)
# Space: O(n)
def isPalindrome(self, s: str) -> bool:
# 1. Convert string
# Time: O(n)
# Space: O(n) since re.sub() will internally use a new string of length n.
s = re.sub('[^a-z0-9]', '', s.lower())
length = len(s)
# 2. Check if the string reads the same forward and backward, one by one.
# Time: O(n)
# Space: O(1)
for i in range(length):
if (s[i] != s[length - 1 - i]):
return False
return True