Minimum Sum of Four Digit Number After Splitting Digits (LeetCode 2160): Solution and Explanation

Easy
Math
Greedy
Sorting

Rearrange the four digits of num into two new numbers, leading zeros allowed, so that their sum is as small as possible.

Intuition

Each digit's contribution to the sum depends only on its place value. A digit in a tens place adds ten times its value, a digit in a ones place adds its value once.

A three-digit number would put some digit in a hundreds place, weighted by 100, so two two-digit numbers are always at least as good. That gives exactly two tens slots and two ones slots.

To minimize the weighted sum, the heaviest weights should multiply the smallest digits. So the two smallest digits go into the tens places and the two largest into the ones places.

After sorting the digits, the sum is (d[0] + d[1]) * 10 + d[2] + d[3]. Pairing them as d[0]d[2] and d[1]d[3] is one convenient way to write it.

Approach: Sort digits and assign by place value

  1. Peel off the four digits with % 10 and / 10, or read them from the string.
  2. Sort the digits in increasing order.
  3. Form new1 = d[0] * 10 + d[2] and new2 = d[1] * 10 + d[3].
  4. Return new1 + new2.

Step-by-step example

num = 2932
  1. The digits are 2, 9, 3, 2.
  2. Sorted: [2, 2, 3, 9].
  3. new1 = 2 * 10 + 3 = 23.
  4. new2 = 2 * 10 + 9 = 29.
  5. The answer is 23 + 29 = 52.

Code: Python, Java, C++

1class Solution:
2    def minimumSum(self, num: int) -> int:
3        d = sorted(int(c) for c in str(num))
4        new1 = d[0] * 10 + d[2]
5        new2 = d[1] * 10 + d[3]
6        return new1 + new2
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1)

There are always exactly four digits, so extracting and sorting them is constant work.

Space

O(1)

Only four digits are stored.

Other approaches

Try every split (time O(1) (24 x 3 splits), space O(1))

Enumerate all 24 orderings of the digits and every cut point, and keep the smallest sum. It is a good check for the greedy but does far more work than needed.

Counting the digits (time O(1), space O(1))

Count how many times each digit 0 to 9 appears and read them out in order instead of sorting. It avoids a sort call but is the same idea.

Common mistakes

  • Putting the two smallest digits in the same number, such as 22 + 39 for 2932. That gives 61, because a large digit ended up in a tens place.
  • Trying a three-digit plus one-digit split. The hundreds place multiplies a digit by 100, which is never better.
  • Worrying about leading zeros. They are allowed, so 4009 becomes 04 + 09 = 13.

Frequently asked questions

Why are two two-digit numbers always best?

Any split uses four place values. Two two-digit numbers use weights 10, 10, 1, 1. Any other split uses a weight of 100 or more somewhere, which can only increase the sum for the same digits.

Does it matter which tens digit goes with which ones digit?

No. The sum is (d[0] + d[1]) * 10 + d[2] + d[3] either way. The pairing d[0]d[2] and d[1]d[3] is just one way to write it.

What if num contains zeros?

Zeros sort to the front and land in the tens places, which is exactly what you want, since leading zeros are allowed.