Minimum Cost to Cut a Stick
The problem is asking us to find the minimum total cost of making the cuts to a stick of length n
. We need to minimize the cost by finding an optimal order in which to make the cuts.
A dynamic programming approach can help us solve this problem. Let’s first sort the cuts and then use a 2D table dp
where dp[i][j]
stores the minimum cost to make all cuts between cuts[i]
and cuts[j]
. We iterate through all possible lengths and all possible starting points for our current segment.
Here’s the code:


The function will return the minimum total cost of the cuts. For example, given n = 7
and cuts = [1,3,4,5]
, it will return 16
.
10 Prerequisite LeetCode Problems
“1547. Minimum Cost to Cut a Stick” falls under the category of dynamic programming and binary search. Here are 10 problems to prepare for this one:
375. Guess Number Higher or Lower II: This problem can give you a sense of how to use dynamic programming to solve problems in the form of “minimize the worstcase cost.”
312. Burst Balloons: This problem has a similar structure where you are trying to find an order of operations that minimizes cost.
1040. Moving Stones Until Consecutive II: This problem is about manipulating a sequence of numbers, which can be helpful practice.
664. Strange Printer: This problem can also help you practice finding the order of operations that minimizes cost.
1000. Minimum Cost to Merge Stones: This problem is about finding an optimal order to perform operations with a cost associated with each operation, which is quite similar to the Cut Stick problem.
1039. Minimum Score Triangulation of Polygon: A problem with a similar structure, where you are trying to divide a shape into smaller parts.
472. Concatenated Words: This problem helps in understanding the basic concepts of dynamic programming and applying it in different scenarios.
410. Split Array Largest Sum: This problem is about binary search on answer and dynamic programming.
546. Remove Boxes: This problem requires understanding of advanced dynamic programming.
132. Palindrome Partitioning II: This problem focuses on how to partition a sequence in a way that minimizes cost.


Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows:
Given an integer array cuts where cuts[i] denotes a position you should perform a cut at.
You should perform the cuts in order, you can change the order of the cuts as you wish.
The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.
Return the minimum total cost of the cuts.
Example 1:
Input: n = 7, cuts = [1,3,4,5] Output: 16 Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:
The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20. Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16). Example 2:
Input: n = 9, cuts = [5,6,1,4,2] Output: 22 Explanation: If you try the given cuts ordering the cost will be 25. There are much ordering with total cost <= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.
Constraints:
2 <= n <= 106 1 <= cuts.length <= min(n  1, 100) 1 <= cuts[i] <= n  1 All the integers in cuts array are distinct.
Language Agnostic Coding Drills
The Python code is an implementation of a classical Dynamic Programming (DP) problem known as the Rod Cutting Problem. It utilizes a topdown DP approach (often called memoization) to find the minimum cost of cutting a rod into specific pieces. This problem is typically solved using recursion, and here the memoization technique is added to avoid repeated computations.
The below learning units help to understand this problem:
 Understanding the basics of classes and methods.
 Basic data structures: lists and 2D lists.
 Variable declarations and assignment, including Python’s negative infinity.
 Control Structures: Conditional statements (
if
), loops (for
,while
).  Understanding of function recursion.
 Understanding slicing operations in lists and the concept of intervals.
 Concept of sorting a list.
 Understanding the memoization technique and the use of DP to avoid repeated computation.
 Analyzing the problem, understanding the rod cutting problem.
 Formulating the recursive function based on problem analysis.
 Implementing the memoization technique in recursion.
 Integrating all of these concepts to solve the problem.
ProblemSolving Approach:
To solve the problem of finding the minimum cost of cutting a rod, the problem can be analyzed as follows:
 Initially, the start and end of the rod are taken as the cutting limits.
 Each possible cutting position between these limits is considered.
 For each cut, the rod is divided into left and right parts. The cost of this cut is the length of the current rod piece.
 Each of these divided parts is again a subrod that needs to be cut in an optimal way. This leads to the recursion: for each left and right subrods, the same cutting process is applied recursively.
 In recursion, the base case is when the left limit exceeds the right limit, which means there’s no rod left to cut, so the cost is 0.
 To avoid recomputation of the same subproblem, a DP table is maintained. If the result of a subproblem has already been computed, it’s directly returned from the DP table.
 The DP table is a 2D list, where dp[i][j] holds the minimum cost to cut the rod between the ith and jth cut positions.
 The recursive process is applied to all possible cut positions, and the minimum cost of all is selected as the answer for the current subproblem.
 The topmost recursion call will eventually return the minimum cost to cut the entire rod.
In this problem, the main complexity lies in the formulation of the recursive relation and the use of memoization to avoid recomputation, which is a common strategy in advanced dynamic programming problems.
Targeted Drills in Python
Understanding the basics of classes and methods:
1 2 3 4 5 6
class MyClass: def my_method(self): print("Hello, World!") obj = MyClass() obj.my_method()
Basic data structures: lists and 2D lists:
1 2 3 4 5
my_list = [1, 2, 3, 4, 5] print(my_list) my_2D_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(my_2D_list)
Variable declarations and assignment, including Python’s negative infinity:
1 2 3
a = 10 b = float('inf') print(a, b)
Control Structures: Conditional statements (
if
), loops (for
,while
):1 2 3 4 5 6 7 8 9 10 11
a = 10 if a > 5: print("a is greater than 5") for i in range(5): print(i) i = 0 while i < 5: print(i) i += 1
Understanding of function recursion:
1 2 3 4 5 6 7
def factorial(n): if n == 0: return 1 else: return n * factorial(n  1) print(factorial(5))
Understanding slicing operations in lists and the concept of intervals:
1 2
my_list = [1, 2, 3, 4, 5] print(my_list[1:4])
Concept of sorting a list:
1 2 3
my_list = [5, 1, 3, 2, 4] my_list.sort() print(my_list)
Understanding the memoization technique and the use of DP to avoid repeated computation:
1 2 3 4 5 6 7 8 9 10 11
dp = [0] * 100 def fibonacci(n): if n <= 1: return n if dp[n] != 0: return dp[n] dp[n] = fibonacci(n  1) + fibonacci(n  2) return dp[n] print(fibonacci(10))
Analyzing the problem, understanding the rod cutting problem. (No drill for this, it’s theoretical)
Formulating the recursive function based on problem analysis. (No drill for this, it’s theoretical)
Implementing the memoization technique in recursion. (Drill for this concept is covered in drill 8)
Integrating all of these concepts to solve the problem. (This is not a separate drill, but the final problem solution)