Problem 359894 · medium · Level 03 Linear Management & Searching

Best Stretch With One Day Off

kadane · state machine · dynamic programming

earnings[i] is what a street musician made on day i (negative on days with fines). She wants to pick a contiguous block of days and may strike out at most one day inside that block; the days that remain must be non-empty. Return the largest total she can report.

Examples

Input:  earnings = [2, -5, 3, 4]
Output: 9
Explanation: Take all four days and strike out the -5.
Input:  earnings = [-1, -2, -3]
Output: -1

Constraints

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

Goals

  • Extend the running-best idea with a second state for 'one element already removed'
  • Keep the removed-element result non-empty
Starting Python…