当前位置:网站首页>The minimum sum of the last four digits of the split digit of leetcode simple problem
The minimum sum of the last four digits of the split digit of leetcode simple problem
2022-07-06 15:03:00 【·Starry Sea】
subject
Here's a four just Integers num . Please use num Medium digit , take num Split into two new integers new1 and new2 .new1 and 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 1:
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 .
Example 2:
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 .
Tips :
1000 <= num <= 9999
source : Power button (LeetCode)
Their thinking
There are only two cases that can form a number pair , One is a number, which is a single digit , The other number is a three digit number , The other case is , Both numbers are double digits . Analyze two situations , Suppose the four digits of the four digits are a,b,c,d, Situation 1 :abc and d, that abc+d=a×100+b×10+c+d; Situation two :ab and cd, that ac+bd=a×10+b×10+c+d. No matter case one or case two, we should minimize their sum , Then the number of high-power positions should be smaller , Then according to situation one or two, let's assume a and b smaller ,c and d more , Obviously, the general situation of situation two is better than that of situation one , Now consider special numbers 0, Suppose the highest power is chosen 0, At best, situation one can only draw with situation two , So we take case two . In case 2, it is still necessary to analyze and select two smaller numbers , There are two larger numbers left , Obviously, let the largest of the two larger numbers follow the smallest of the two smaller numbers , The smaller of the two larger numbers is best followed by the larger of the two smaller numbers .
class Solution:
def minimumSum(self, num: int) -> int:
temp=[]
while num>0: # Split digit
temp.append(num%10)
num//=10
temp.sort()
return temp[0]*10+temp[1]*10+temp[2]+temp[3]

边栏推荐
- “人生若只如初见”——RISC-V
- "If life is just like the first sight" -- risc-v
- Matplotlib绘图快速入门
- [issue 18] share a Netease go experience
- How to transform functional testing into automated testing?
- Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
- [pointer] octal to decimal
- [pointer] counts the number of times one string appears in another string
- 后台登录系统,JDBC连接数据库,做小案例练习
- Common Oracle commands
猜你喜欢

Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025

STC-B学习板蜂鸣器播放音乐

Cc36 different subsequences

Database monitoring SQL execution

About the garbled code problem of superstar script

Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary

CSAPP家庭作业答案7 8 9章

ucore lab1 系统软件启动过程 实验报告

Want to learn how to get started and learn software testing? I'll give you a good chat today

C language do while loop classic Level 2 questions
随机推荐
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
“Hello IC World”
Interview Essentials: what is the mysterious framework asking?
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
函数:求两个正数的最大公约数和最小公倍
Keil5 MDK's formatting code tool and adding shortcuts
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
Fundamentals of digital circuits (I) number system and code system
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
数字电路基础(五)算术运算电路
Description of Vos storage space, bandwidth occupation and PPS requirements
数据库多表链接的查询方式
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
JDBC 的四种连接方式 直接上代码
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
Cc36 different subsequences
Wang Shuang's detailed learning notes of assembly language II: registers
关于交换a和b的值的四种方法