Maximum Product Subarray
Return the largest product of a nonempty contiguous subarray.
Example 1
nums = [-2,3,-4]24Explanation
The full product -23-4=24 becomes positive from two negative factors.
Example 2
nums = [-2,0,-1]0Explanation
Zero beats either negative singleton and prevents a positive full-range product.
Example 3
nums = [2,3,-2,4]6Explanation
The prefix 2*3=6 is best; extending through -2 changes its sign.
Constraints
- 1 <= len(nums) <= 20000
- Values are -10..10
- Products of all contiguous subarrays fit signed 32-bit integers
Function contract
maxProductArguments: jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 34 cases, on your device.
[-2,3,-4]