InferenceAI

Enter at least two characters to search.

Start free

Maximum Product Run

mediumArrays & Strings25 min25 XP

Return the largest product of a non-empty contiguous subarray of nums.

Requirements

  • O(n)O(n): track both the maximum and minimum product ending at each position, because a negative number swaps them.

Examples

EXAMPLE WALKTHROUGHskip the negative

A guided view of the published example, not a trace of your code.

INPUT[[2,3,-2,4]]
TASKmax_product_run()
EXPECTED OUTPUT6

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [2,3,-2,4]

Output: 6

Explanation: skip the negative

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

Output: 0

Explanation: zero

Input: nums = [-2,3,-4]

Output: 24

Explanation: two negatives

Hint 1

At each step the new max is the largest of x, x * max, x * min.

Hint 2

Zeros reset both products.

Topics

DSAArrays & StringsPython