class Solution: def specialArray(self, A: List[int]) -> int: """ tc O(lgN) sc O(1) a is non-negtive => x range is [1,n], check edge case when x = 0, should return -1 """ #A.sort() not necessary since x is correlated with size of A instead of value n = len(A) l,r...
[Read More]
1658. Minimum Operations to Reduce X to Zero
```python class Solution: def minOperations(self, A: List[int], x: int) -> int: “"”tc O(N) sc O(N) find subarray A(l:r] s.t. its length is max and total - sum(A(l:r]) == x => total - x == sum(A(l:r]) => psum(A[r]) - psum[A[l]] => we keep record a presum with its no have to...
[Read More]
1590. Make Sum Divisible by P
```python class Solution: def minSubarray(self, A: List[int], p: int) -> int: “”” tc O(N) sc O(N) case1 no need to remove sum(A) % p == 0 case2 there is a min length subarray A[i:j], => [total - sum(A[i:j])] % p == 0 ==> total%p == sum(A[i:j])%p = rem ==> 1....
[Read More]
1542. Find Longest Awesome Substring
```python class Solution: def longestAwesome(self, s: str) -> int: “”” tc O(KN) sc O(2^K), K: unique characters in string, here K = 10 Note, since palindrom allow up to one number with odd count , check all masks different from current one by one bit => if two masks are...
[Read More]
1567. Maximum Length of Subarray With Positive Product
```python class Solution: def getMaxLen(self, A: List[int]) -> int: “"”tc O(N) sc O(1) avoid 0 => 0 split A into subarray record first neg pos main idea: for each non-zero number, check current accumutive neg cnt to decide if we need to reduce possible max subarray length by 1 each...
[Read More]