当前位置:网站首页>2160. minimum sum of the last four digits after splitting

2160. minimum sum of the last four digits after splitting

2022-06-25 07:50:00 AlbertOS

introduce

Here's a four just Integers n u m num num . Please use n u m num num Medium digit , take n u m num num Split into two new integers n e w 1 new1 new1 and n e w 2 new2 new2 .
n e w 1 new1 new1 and n e w 2 new2 new2 You can have Leading 0 , And num in all All digits must be used .
For example , Here you are. num = 2932 , The numbers you have include : Two 2 , One 9 And a 3 . Some of the possibilities [new1, new2] The number pair is [22, 93],[23, 92],[223, 9] and [2, 329] .
Please return what you can get new1 and new2 Of Minimum and .

Example

Input :num = 2932
Output :52
explain : feasible [new1, new2] The number pair is [29, 23] ,[223, 9] wait .
The minimum sum is a number pair [29, 23] And :29 + 23 = 52 .

Input :num = 4009
Output :13
explain : feasible [new1, new2] The number pair is [0, 49] ,[490, 0] wait .
The minimum sum is a number pair [4, 9] And :4 + 9 = 13 .

Answer key

The meaning of this topic will be clear from the examples , Is a four digit positive integer , It means finding the two smallest tens in this integer as the first and second number pairs , All that remains is a bit of both , This is the minimum sum .
The obvious greedy method , We put the current smaller value at a higher level , Store... In an array n u m num num The value of each of the , Then sort in ascending order , Then the minimum sum is 10 ∗ ( d i g [ 0 ] + d i g [ 1 ] ) + ( d i g [ 2 ] + d i g [ 3 ] ) 10 * (dig[0]+dig[1])+(dig[2]+dig[3]) 10(dig[0]+dig[1])+(dig[2]+dig[3])

class Solution {
    
public:
    int minimumSum(int num) {
    
        vector<int> digits;
        // Store each numeric bit in an array 
        while (num) {
    
            digits.push_back(num % 10);
            num /= 10;
        }
        // Array ascending sort 
        sort(digits.begin(), digits.end());
        return 10 * (digits[0] + digits[1]) + digits[2] + digits[3];
    }
};
原网站

版权声明
本文为[AlbertOS]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250550167008.html