Find the contiguous subarray with the largest product and return that product.
A negative number turns the smallest product into the largest. Keep both the max and min product ending at each index.
1function maxProduct(nums: number[]): number {2let hi = nums[0], lo = nums[0], best = nums[0];3for (let i = 1; i < nums.length; i++) {4const x = nums[i];5const candidates = [x, hi * x, lo * x];6hi = Math.max(...candidates);7lo = Math.min(...candidates);8best = Math.max(best, hi);9}10return best;11}
hi = lo = best = 2.
Space: play/pause · ←/→: step