diff --git a/maximum-product-subarray/joon.py b/maximum-product-subarray/joon.py new file mode 100644 index 000000000..8cc5a6a28 --- /dev/null +++ b/maximum-product-subarray/joon.py @@ -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) diff --git a/missing-number/joon.py b/missing-number/joon.py new file mode 100644 index 000000000..11d053b5f --- /dev/null +++ b/missing-number/joon.py @@ -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) diff --git a/valid-palindrome/joon.py b/valid-palindrome/joon.py new file mode 100644 index 000000000..b9bed8246 --- /dev/null +++ b/valid-palindrome/joon.py @@ -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