algorythms
Dynamic Programming
LC #152Medium

Maximum Product Subarray

Dynamic Programming
AmazonLinkedInGoogleMeta

Problem

Find the contiguous subarray with the largest product. The array may contain negative numbers and zeros.

arraydynamic-programming

Constraints

  • 1 ≤ nums.length ≤ 2×10⁴
  • -10 ≤ nums[i] ≤ 10
  • Product fits in a 32-bit integer

Example

Inputnums = [2, 3, -2, 4]
Output6
Why

[2,3] has the largest product = 6

Hints — reveal one at a time