Integer Break (LeetCode 343): Solution and Explanation

Medium
Math
Dynamic Programming

Split a positive integer n (2 to 58) into at least two positive integers that add up to n, and return the largest possible product of those integers. Think of it as cutting a rope of length n into pieces whose lengths multiply to the most.

Intuition

Picture a rope of length i. Any way of cutting it has a first piece of some length j, and what is left is a rope of length i - j.

That leftover rope has two options. It can stay as one piece, giving j * (i - j). Or it can be cut further, and then the best it can do is dp[i - j], giving j * dp[i - j].

Why compare both? dp[k] forces at least one cut, and for small lengths a cut hurts: dp[2] = 1 and dp[3] = 2, but a leftover 2 or 3 kept whole is worth 2 or 3. For longer leftovers, cutting wins.

Trying every first piece j covers every possible set of cuts, so dp[i] is the maximum over j of those two products. Filling dp from 2 up to n makes every dp[i - j] ready when it is needed.

Approach: 1D DP over the length

  1. Create dp of size n + 1 and set dp[1] = 1.
  2. For i from 2 to n, and for every first piece j from 1 to i - 1:
  3. Set dp[i] = max(dp[i], j * (i - j), j * dp[i - j]).
  4. Return dp[n].

Step-by-step example

n = 10
  1. Small values: dp[2] = 1 (1+1), dp[3] = 2 (1+2), dp[4] = 4 (2+2), dp[5] = 6 (2+3), dp[6] = 9 (3+3).
  2. dp[7] = 12: first piece 3 with 4 kept whole gives 12, and first piece 4 with 3 whole also gives 12.
  3. dp[8] = 18 (3 × 3 × 2) and dp[9] = 27 (3 × 3 × 3).
  4. dp[10]: first piece 3 and the rest broken as dp[7] = 12 gives 36, and nothing beats it. The cut is 3 + 3 + 4, product 36.

Code: Python, Java, C++

1class Solution:
2    def integerBreak(self, n: int) -> int:
3        dp = [0] * (n + 1)
4        dp[1] = 1
5        for i in range(2, n + 1):
6            for j in range(1, i):
7                dp[i] = max(dp[i], j * (i - j), j * dp[i - j])
8        return dp[n]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n^2)

For each length i the inner loop tries i - 1 first pieces.

Space

O(n)

One array of n + 1 best products.

Other approaches

Greedy with 3s (math) (time O(n), or O(log n) with fast power, space O(1))

For n > 4, cut off 3s while more than 4 remains, then multiply by the remainder (2, 3 or 4). A piece of 4 or more can be split into 2 and the rest without losing product, and three 2s (8) lose to two 3s (9), so 3s win. Handle n = 2 and n = 3 separately (answers 1 and 2).

DP with only 2 and 3 as the first piece (time O(n), space O(n))

Since the best cut never needs a piece larger than 3, dp[i] = max(2 * max(i - 2, dp[i - 2]), 3 * max(i - 3, dp[i - 3])). Same idea as the full DP with a constant-size inner loop.

Common mistakes

  • Forgetting the leftover can stay whole. Using only j * dp[i - j] gives dp[4] = 2 * dp[2] = 2 instead of 4.
  • Returning n itself for small inputs. At least two pieces are required, so n = 2 gives 1 and n = 3 gives 2.
  • Greedy with a leftover 1. For n = 4 the 3s greedy would give 3 × 1 = 3; the remainder rule must turn 3 + 1 into 2 + 2.
  • Overflow worries. The largest answer, for n = 58, is 1,549,681,956, which still fits in a 32-bit signed integer.

Frequently asked questions

Why are 3s the best pieces?

Any piece of 5 or more can be split into 2 and k - 2 with 2(k - 2) > k, so large pieces never help. A 4 equals 2 × 2. Among 2s and 3s, six units as 3 + 3 give 9 while 2 + 2 + 2 give 8, so you want as many 3s as possible, with one or two 2s to absorb the remainder.

Why is dp[1] = 1 when 1 cannot be split?

It is only a building block. A leftover of length 1 contributes a factor of 1, and j * dp[1] is the same as keeping the 1 whole, so the value does not distort any answer.

Does j need to go all the way to i - 1?

No. By symmetry j up to i / 2 is enough when you also compare the leftover kept whole, and in fact j of 2 or 3 is enough. Looping to i - 1 is just the simplest correct version.