当前位置:网站首页>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]

边栏推荐
- Function: find the root of the equation by Newton iterative method
- Function: find the maximum common divisor and the minimum common multiple of two positive numbers
- 5 minutes to master machine learning iris logical regression classification
- [HCIA continuous update] advanced features of routing
- [oiclass] maximum formula
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
- Oracle foundation and system table
- Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- “人生若只如初见”——RISC-V
猜你喜欢

Wang Shuang's detailed notes on assembly language learning I: basic knowledge

ES全文索引

Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class

“Hello IC World”

王爽汇编语言学习详细笔记一:基础知识

四元数---基本概念(转载)

Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers

ucore lab2 物理内存管理 实验报告

Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator

关于交换a和b的值的四种方法
随机推荐
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
数字电路基础(四) 数据分配器、数据选择器和数值比较器
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
Stc-b learning board buzzer plays music 2.0
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
Install and run tensorflow object detection API video object recognition system of Google open source
“人生若只如初见”——RISC-V
[HCIA continuous update] working principle of static route and default route
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
Don't you even look at such a detailed and comprehensive written software test question?
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
函数:字符串反序存放
STC-B学习板蜂鸣器播放音乐2.0
Four methods of exchanging the values of a and B
Express
Flash implements forced login
[HCIA continuous update] advanced features of routing
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
Face and eye recognition based on OpenCV's own model