Minimum Sum of Four Digit Number After Splitting Digits (LeetCode 2160): Solution and Explanation
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
- Peel off the four digits with
% 10and/ 10, or read them from the string. - Sort the digits in increasing order.
- Form
new1 = d[0] * 10 + d[2]andnew2 = d[1] * 10 + d[3]. - Return
new1 + new2.
Step-by-step example
num = 2932
- The digits are 2, 9, 3, 2.
- Sorted: [2, 2, 3, 9].
- new1 = 2 * 10 + 3 = 23.
- new2 = 2 * 10 + 9 = 29.
- 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 + new21class Solution {
2 public int minimumSum(int num) {
3 int[] d = new int[4];
4 for (int i = 0; i < 4; i++) {
5 d[i] = num % 10;
6 num /= 10;
7 }
8 Arrays.sort(d);
9 int new1 = d[0] * 10 + d[2];
10 int new2 = d[1] * 10 + d[3];
11 return new1 + new2;
12 }
13}1class Solution {
2public:
3 int minimumSum(int num) {
4 vector<int> d;
5 for (int i = 0; i < 4; i++) {
6 d.push_back(num % 10);
7 num /= 10;
8 }
9 sort(d.begin(), d.end());
10 int new1 = d[0] * 10 + d[2];
11 int new2 = d[1] * 10 + d[3];
12 return new1 + new2;
13 }
14};Time and space complexity
O(1)
There are always exactly four digits, so extracting and sorting them is constant work.
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.