Maximum Product Subarray
Given an integer array nums, find the contiguous, non-empty subarray whose elements have the largest product, and return that product.
The array can contain negative numbers and zeros, so the best subarray is not always the longest one, and the answer may be 0 or negative (for example when the array is a single negative number).
Constraints
1 <= nums.length <= 2 * 10^4-10 <= nums[i] <= 10- The product of any subarray fits in a 32-bit integer.
Examples
Example 1
Input: nums = [2,3,-2,4] Output: 6
The subarray [2, 3] has product 6. Including -2 flips the sign and 4 cannot fix it.
Example 2
Input: nums = [-2,0,-1] Output: 0
The zero splits the array. The best is the single value 0, since -2 and -1 are not adjacent.
Example 3
Input: nums = [-2,3,-4] Output: 24
Two negatives multiply to a positive: -2 * 3 * -4 = 24.
JavaScript you'll use: Arrays: create, read, update Numbers, Math and integer tricks
/**
* @param {number[]} nums
* @return {number}
*/
function maxProduct(nums) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.