210. Maximum Product Subarray

Medium
Dual Running Extremes
Array

Problem

Given an integer array nums, find the contiguous non-empty subarray that has the largest product and return that product.

Examples

Example 1

Input: [2,3,-2,4]
Output: 6

Example 2

Input: [-2,0,-1]
Output: 0

Example 3

Input: [-2,3,-4]
Output: 24
Constraints

1 <= nums.length <= 20000

-10 <= nums[i] <= 10

The answer fits in the numeric range used by the judge.

Hints

?? A negative number can turn the smallest product into the largest.

?? Track both maximum and minimum products ending at the current index.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Why is tracking only the maximum product insufficient?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[2,3,-2,4]
Expected
6