File tree 3 files changed +48
-0
lines changed
3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments