Problem 393586 · medium · Phase 03 Linear Management & Searching

Most Valuable Product Stretch

kadane · products · sign handling

A row of dials shows small integers dials (some negative, some zero). Choose a non-empty contiguous stretch of dials and multiply their values together. Return the largest product you can obtain.

Examples

Input:  dials = [3, -1, 4, -2, 0, 5]
Output: 24
Explanation: 3 * -1 * 4 * -2 = 24; the two negatives cancel.
Input:  dials = [-4]
Output: -4

Constraints

  • 1 <= len(dials) <= 5 * 10**4, -9 <= dials[i] <= 9.
  • Target complexity: O(n) time, O(1) extra space.

Goals

  • Track both the largest and the smallest product ending at each index
  • Understand why a negative factor swaps the roles of max and min
Starting Python…