Given an integer array nums, return the largest product of any contiguous non-empty subarray. Negatives are the whole game: two of them multiply into a positive, so the worst product so far can become the best one element later.
Examples
Example 1:
Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3].
Example 2:
Input: nums = [-2,0,-1]
Output: 0
Example 3:
Input: nums = [-2,3,-4]
Output: 24
Explanation: the whole array — the two negatives cancel.
Example 4:
Input: nums = [-2]
Output: -2
Constraints
1 <= nums.length <= 2 * 10^4
-10 <= nums[i] <= 10
Every subarray product fits in a 32-bit integer.
Prerequisites
Dynamic programming — being able to say, in one sentence, what a single cell of your table MEANS. Everything else follows from that sentence. Rolling variables: when a state only looks back one or two steps, the table collapses to a couple of numbers.
How to think about it
1. Keep the Biggest and Smallest Product Ending Here Optimal
Intuition
Every subarray ends at some number, so walk the numbers once and, at each one, ask for the biggest product of a subarray ending right there. That subarray is the number alone, or the one ending just before it times the number. Times a positive, the biggest product stays the biggest; times a negative, the order flips, and the smallest becomes the biggest. So carry two products, big and small, the biggest and the smallest ending here, and keep the biggest big ever seen as best. Keeping only the biggest is not that: on 2, -3, -2 and 4 it throws away the -6 that the second negative turns into 12, and finds 24 where the whole array makes 48. Keep the biggest and smallest product ending here; a negative swaps them.
Algorithm
1. Start best, big and small at the first number: on its own, it is both the biggest and the smallest product ending there. 2. For every number x after it, if x is negative, swap big and small first: multiplying by it flips their order. 3. big becomes the larger of x alone and big * x, and small the smaller of x alone and small * x. x alone wins when the run so far only hurts, which is also how a zero starts everything again. 4. best keeps the larger of itself and big. 5. After the last number, return best.
Time & Space
Time O(n): every number is visited once. Space O(1): only best, big and small are kept.
2. Carry the Smallest Too Optimal
Intuition
With sums, the worst running total is useless; with products it is the most valuable thing you have, because one negative number turns the smallest product into the largest. So carry both the largest and smallest product ending here — and when the current number is negative, swap their roles before extending, since multiplying flips the order.
Algorithm
1. Start both running products at the first element. 2. For each next number, swap the running maximum and minimum if it is negative. 3. Extend each by multiplying, or restart from the number itself — whichever is better. 4. Track the largest value ever held.