Ctrl K

Biggest Swing of a Stretch

Medium
The array changes holds day-by-day changes of a price. The swing of a stretch of days is the absolute value of the sum of changes over that stretch (a stretch is a contiguous part of the array, possibly empty; an empty stretch has a swing of 0). Return the largest swing over all stretches, whether it is a rise or a fall.
Example 1
changes=[3, -7, 2, -6, 4] → 11

The stretch [-7, 2, -6] sums to -11, whose absolute value is 11. No stretch does better.

Example 2
changes=[5, 1, -2, 6] → 10

The whole array: 5 + 1 - 2 + 6 = 10.

Example 3
changes=[0, 0] → 0
Constraints
  • 1 ≤ len(changes) ≤ 10⁵
  • -10⁴ ≤ changes[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
changes=[3, -7, 2, -6, 4]
Expected
11

Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.