Skip to main content

Minimum Cost to Split into Ones - Solution & Explanation

MediumMathDynamic Programming6 min readAsked at: Amazon
Practice this problem

Problem Statement

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 <= 500

Approach Overview

Problem 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.

Solution

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Dynamic Programming SimulationO(n^2)O(n)Useful for reasoning about the recurrence or when deriving the formula from first principles
Mathematical ObservationO(1)O(1)Optimal approach; compute the result directly using the split-count insight

Video Solution

Minimum Cost to Split into Ones | LeetCode 3857 | Weekly Contest 491 | Java | Developer Coder • Developer Coder • 319 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Minimum Cost to Split into Ones easy or hard?
The problem is usually categorized as Medium because many candidates initially model it with dynamic programming. Once the invariant about the number of splits is recognized, the final solution becomes a simple constant-time formula.
Minimum Cost to Split into Ones Python/Java solution
The implementation is extremely short because the answer depends only on n. Return n - 1 using basic arithmetic. The same logic works identically in Python, Java, C++, Go, and TypeScript.
How to solve Minimum Cost to Split into Ones in O(1)?
Observe that converting a single number into n pieces of value 1 requires exactly n-1 splits. Since each split contributes deterministically to the total cost, the minimum cost simplifies to n-1. The implementation only performs a constant-time arithmetic computation.
What is the best approach for Minimum Cost to Split into Ones?
The optimal approach uses a mathematical observation instead of full dynamic programming. Splitting a number into n ones always requires exactly n-1 operations because each split increases the number of pieces by one. Using this invariant, the minimum total cost can be computed directly in O(1) time and O(1) space.
Is Minimum Cost to Split into Ones asked at Google/Amazon/Meta?
Problems based on mathematical observations and dynamic programming reductions frequently appear in interviews at companies like Google, Amazon, and Meta. Interviewers use them to test whether candidates can simplify a DP recurrence into a direct formula.
What data structure is used in Minimum Cost to Split into Ones?
The optimal solution does not require any data structures. It relies on a mathematical invariant about the number of splits. A conceptual dynamic programming formulation may use an array dp[n], but it becomes unnecessary after deriving the closed-form result.
What is the time complexity of Minimum Cost to Split into Ones?
The optimal solution runs in O(1) time and O(1) space because the answer can be derived directly from the value of n. A naive dynamic programming formulation that tests every partition would take O(n^2) time and O(n) space.

Ready to solve this problem?

Practice Minimum Cost to Split into Ones with our built-in code editor and test cases.

Practice on FleetCode