[LeetCode]#2160. Minimum Sum of Four Digit Number After Splitting Digits
2 min readNov 2, 2022
Environment: Python 3.8
Key technique: sorted
You are given a positive integer num
consisting of exactly four digits. Split num
into two new integers new1
and new2
by using the digits found in num
. Leading zeros are allowed in new1
and new2
, and all the digits found in num
must be used.
- For example, given
num = 2932
, you have the following digits: two2
's, one9
and one3
. Some of the possible pairs[new1, new2]
are[22, 93]
,[23, 92]
,[223, 9]
and[2, 329]
.
Return the minimum possible sum of new1
and new2
.
Example 1:
Input: num = 2932
Output: 52
Explanation: Some possible pairs [new1, new2] are [29, 23], [223, 9], etc.
The minimum sum can be obtained by the pair [29, 23]: 29 + 23 = 52.
Analysis:
- Sort each number from input.
- The minimum should be as below. (1st+3rd) pair + (2nd + 4th) pair.
Solution:
class Solution:
def minimumSum(self, num):
ans=sorted(str(num))
return int(ans[0]+ans[2])+int(ans[1]+ans[3])
Submissions:
Reference: