Skip to content
BASA DSA

Maximum Product Subarray

Medium1-D Dynamic Programming

Return the largest product of a nonempty contiguous subarray.

Example 1

Input
nums = [-2,3,-4]
Output
24

Explanation

The full product -23-4=24 becomes positive from two negative factors.

Example 2

Input
nums = [-2,0,-1]
Output
0

Explanation

Zero beats either negative singleton and prevents a positive full-range product.

Example 3

Input
nums = [2,3,-2,4]
Output
6

Explanation

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 contractmaxProductArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…