Skip to the code
cf.
+50 XP

#546 Cheapest Way to Split a Timeline

hard

A video editor has a timeline from 0 to length seconds and must split it at every position listed in cuts. Each split works on one piece at a time and costs the length of the piece being split. You may make the splits in any order.

Write cheapestSplits(length, cuts) that returns the smallest total cost of making every split.

Example 1

Input: length = 6, cuts = [1,3]
Output: 9

Split at 3 first (piece of 6), then at 1 (piece of 3): 6 + 3. Splitting at 1 first costs 6 + 5.

Example 2

Input: length = 5, cuts = [3]
Output: 5

One split of the whole timeline.

Constraints

  • 2 <= length <= 10^6
  • 1 <= cuts.length <= 100
  • 0 < cuts[i] < length, all different, in any order
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).