You are given an integer n.
In one operation, you may split an integer x into two positive integers a and b such that a + b = x.
The cost of this operation is a * b.
Return an integer denoting the minimum total cost required to split the integer n into n ones.
Example 1:
Input: n = 3
Output: 3
Explanation:
One optimal set of operations is:
x |
a |
b |
a + b |
a * b |
Cost |
|---|---|---|---|---|---|
| 3 | 1 | 2 | 3 | 2 | 2 |
| 2 | 1 | 1 | 2 | 1 | 1 |
Thus, the minimum total cost is 2 + 1 = 3.
Example 2:
Input: n = 4
Output: 6
Explanation:
One optimal set of operations is:
x |
a |
b |
a + b |
a * b |
Cost |
|---|---|---|---|---|---|
| 4 | 2 | 2 | 4 | 4 | 4 |
| 2 | 1 | 1 | 2 | 1 | 1 |
| 2 | 1 | 1 | 2 | 1 | 1 |
Thus, the minimum total cost is 4 + 1 + 1 = 6.
Constraints:
1 <= n <= 500Problem Overview: You are given an integer n. In one operation you split a number into two positive integers whose sum equals the original number. Each split incurs a cost equal to the value being split. The goal is to keep splitting until every piece becomes 1, while minimizing the total cost.
Approach 1: Dynamic Programming Simulation (O(n^2) time, O(n) space)
A straightforward way to reason about the problem is to treat it as a dynamic programming task. Let dp[x] represent the minimum cost to split the value x into all ones. For every x, try every possible split x = a + b. The total cost becomes x + dp[a] + dp[b] because the act of splitting x costs x. Iterate through all partitions and keep the minimum. This method clearly models the optimal substructure but quickly becomes inefficient since every state tries up to x-1 partitions.
Approach 2: Mathematical Insight (O(1) time, O(1) space)
The key observation comes from looking at how many splits are actually required. To convert a single number n into n ones, you must perform exactly n - 1 splits. Every split increases the number of pieces by exactly one. Starting with one piece and ending with n pieces means n - 1 operations regardless of how you partition the numbers.
Each operation costs the value being split. If you examine any valid sequence of splits, every intermediate piece ultimately contributes exactly once to the total cost before it breaks down further. Summing across the process leads to a deterministic total: the minimum total cost simplifies to n - 1. This eliminates the need for simulation, recursion, or state storage. The result can be computed directly with simple arithmetic, making it a pure math observation rather than a full dynamic programming implementation.
Recommended for interviews: Start by describing the DP formulation to show you understand the optimal substructure and recurrence. Then derive the mathematical simplification. Interviewers usually expect the constant-time solution because recognizing the invariant (exactly n-1 splits) demonstrates stronger problem-solving intuition.
To minimize the total cost, we first split n into 1 and n-1, with a cost of 1 cdot (n-1) = n-1. Next, we split n-1 into 1 and n-2, with a cost of 1 cdot (n-2) = n-2.
We continue this process until we split 2 into 1 and 1, with a cost of 1 cdot 1 = 1. Therefore, the total cost is (n-1) + (n-2) + ldots + 2 + 1 = \frac{n(n-1)}{2}.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming Simulation | O(n^2) | O(n) | Useful for reasoning about the recurrence or when deriving the formula from first principles |
| Mathematical Observation | O(1) | O(1) | Optimal approach; compute the result directly using the split-count insight |
Minimum Cost to Split into Ones | LeetCode 3857 | Weekly Contest 491 | Java | Developer Coder • Developer Coder • 285 views views
Watch 7 more video solutions →Practice Minimum Cost to Split into Ones with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor