当前位置:网站首页>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];
}
};
边栏推荐
- AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
- CAN总线工作状况和信号质量“体检”
- Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
- 2265. 统计值等于子树平均值的节点数
- 饮食干预减轻癌症治疗相关症状和毒性
- [little knowledge] PCB proofing process
- C WinForm panel custom picture and text
- RTKLIB-b33版本中GALILEO广播星历存储问题
- Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
- Bicubic difference
猜你喜欢
VectorDraw Web Library 10.10
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
How to use ad wiring for PCB design?
Fairmot yolov5s to onnx
STL tutorial 4- input / output stream and object serialization
神经网络与深度学习-3- 机器学习简单示例-PyTorch
Cifar-10 dataset application: quick start data enhancement method mixup significantly improves image recognition accuracy
"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
“空间转换”显著提升陡崖点云的地面点提取质量
(tool class) use SecureCRT as the communication medium
随机推荐
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
LeetCode_哈希表_中等_454.四数相加 II
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
传统的IO存在什么问题?为什么引入零拷贝的?
“空间转换”显著提升陡崖点云的地面点提取质量
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
Accès à la boîte aux lettres du nom de domaine Lead à l'étranger
VSCode很好,但我以后不会再用了
WinForm implementation window is always at the top level
lebel只想前面有星号,但是不想校验
opencv最小值滤波(不局限于图像)
判断用户是否是第一次进入某个页面
@The difference between resource and @autowired annotation, why recommend @resource?
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
双三次差值bicubic
JDBC-DAO层实现
Requirements for Power PCB circuit board design 2021-11-09
Runtime - Methods member variable, cache member variable
Different paths ii[dynamic planning improvement for DFS]
Leetcode daily question - 515 Find the maximum value in each tree row