2-D Dynamic Programming Splitting a range
575. Minimum Cost to Cut a Stick
You have a wooden stick of length n, marked from 0 at one end to n at the other. The array cuts
lists the marks where the stick must be cut. You make the cuts one at a time, in any order you choose.
A cut splits one piece of the stick into two, and it costs the length of that piece at the moment you cut it. So the same cut can cost more or less depending on which cuts were made before it.
Return the smallest total cost of making every cut in cuts.
Example 1
Input: n = 10, cuts = [7, 2, 4]
20Explanation: Cut at 4 first, for 10. Then cut the piece from 0 to 4 at 2, for 4, and the piece from 4 to 10 at 7, for 6. That is 20. Cutting from left to right would cost 10 + 8 + 6 = 24.
Example 2
Input: n = 7, cuts = [3]
7Explanation: A single cut is made on the whole stick, so it costs 7.
Example 3
Input: n = 1000000000
cuts = [125000000, 250000000, 375000000, 500000000, 625000000, 750000000, 875000000]
3000000000Explanation: Cut at the middle, then at the middles of the two halves, then at the middles of the four quarters. Each round costs 109 in all, so the total is 3 × 109.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
2 ≤ n ≤ 1091 ≤ cuts.length ≤ min(n − 1, 500)1 ≤ cuts[i] ≤ n − 1- All the values in
cutsare distinct.
Clarifying questions
What you would ask an interviewer before writing any code.