ADDP-P3

Cutting a rail

Practice 3 of 6Advanced DP11 tests

The problem

A rail runs from 0 to length and must be cut at every position in marks (different whole numbers strictly between 0 and length, in any order). A cut splits one piece into two, and costs the length of the piece being cut. The order of the cuts changes the total. Return the smallest total.

cheapest_cuts(10, [4])           # 10
cheapest_cuts(7, [1, 3, 4, 5])   # 16: cut at 3, 1, 5, then 4

Limits

  • Up to 150 marks; the length can be up to 10⁹.
  • Trying every order of the cuts is far too slow. Memoised recursion is fine here: it goes at most 150 calls deep.

Builds on ADDP-3 Interval DP.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

11 tests, not run yet
Test results
#TestResultReads · time
1No marksNot run
2One markNot run
3Two marksNot run
4The order mattersNot run
5Marks out of orderNot run
6Middle first is not bestNot run
7Marks near the endsNot run
8Twelve marksNot run
9Twenty marksNot run
10150 marksNot run
11150 evenly spaced marksNot run