Problem 388488 · hard · Phase 03 Linear Management & Searching

Two Separate Best Stretches

kadane · prefix and suffix · dynamic programming

sales[i] is a shop's net result on day i. The owner wants to highlight two stretches of consecutive days that do not overlap (they may be adjacent), each non-empty, with the largest combined total. Return that maximum total.

Examples

Input:  sales = [1, -2, 3, -1, 4, -5, 2]
Output: 8
Explanation: Days 2-4 total 6 and day 6 totals 2.
Input:  sales = [-3, -1, -2]
Output: -3
Explanation: Both stretches must be non-empty; -1 and -2 is the best pair.

Constraints

  • 2 <= len(sales) <= 10**5, -10**4 <= sales[i] <= 10**4.
  • Target complexity: O(n) time, O(n) space.

Goals

  • Precompute the best subarray within every prefix and every suffix
  • Combine them across a split point to enforce non-overlap
Starting Python…