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

边栏推荐
- Logstack introduction and deployment -- elasticstack (elk) work notes 019
- [Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
- [pointer] find the largest string
- 【指针】求字符串的长度
- Keil5-MDK的格式化代码工具及添加快捷方式
- Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
- DVWA exercise 05 file upload file upload
- [HCIA continuous update] advanced features of routing
- 【指针】查找最大的字符串
猜你喜欢

Express

HackTheBox-Emdee five for life

王爽汇编语言详细学习笔记二:寄存器

Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class

Install and run tensorflow object detection API video object recognition system of Google open source

Get started with Matplotlib drawing

High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters

Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code

Fundamentals of digital circuits (I) number system and code system

后台登录系统,JDBC连接数据库,做小案例练习
随机推荐
Pointer -- output all characters in the string in reverse order
5 minutes to master machine learning iris logical regression classification
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
函数:字符串反序存放
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
Fundamentals of digital circuits (II) logic algebra
[HCIA continuous update] advanced features of routing
Quaternion -- basic concepts (Reprint)
JDBC 的四种连接方式 直接上代码
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
【指针】求解最后留下的人
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
“Hello IC World”
【指针】统计一字符串在另一个字符串中出现的次数
ucore lab2 物理内存管理 实验报告
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Public key box
Want to learn how to get started and learn software testing? I'll give you a good chat today
CSAPP homework answers chapter 789