/152. Maximum Product Subarray

152. Maximum Product Subarray

Medium
Arrays36.1% acceptance

Given an integer array arr, return the maximum product of any contiguous subarray within arr. The product of a subarray is defined as the multiplication of all its elements. The subarray must be non-empty.

Example 1

Input: [1,-2,3,4,-1]

Output: 12

Explanation: The subarray [3,4] yields the maximum product 12.

Example 2

Input: [-3,-2,-1]

Output: 6

Explanation: The subarray [-3,-2,-1] yields the maximum product 6.

Example 3

Input: [0,2,-3,0,4]

Output: 4

Explanation: The subarray [4] yields the maximum product 4.

Constraints

  • 1 <= len(arr) <= 20000
  • -10 <= arr[i] <= 10 for all i
  • The product of any subarray fits in a 32-bit integer
Python (current runtime)

Case 1

Input: [5,-1,2,0,-3,4]

Expected: 5

Case 2

Input: [-2,-3,7,-1,2]

Expected: 42

Case 3

Input: [0,0,0]

Expected: 0

Case 4

Input: [9]

Expected: 9

Case 5

Input: [-2,3,-4]

Expected: 24