当前位置:网站首页>LeetCode 5218. Sum of integers with K digits (enumeration)
LeetCode 5218. Sum of integers with K digits (enumeration)
2022-06-22 01:28:00 【Michael Amin】
List of articles
1. subject
Here are two integers num and k , Consider a positive integer multiset with the following properties :
- Every integer digit is k .
- The sum of all integers is num .
Returns the Minimum size , If there are no such multiple sets , return -1 .
Be careful :
A multiset is similar to a set , But a multiset can contain more than one integer , The sum of empty multisets is 0 .
One digit number Is the rightmost digit of the number .
Example 1:
Input :num = 58, k = 9
Output :2
explain :
Multiple sets [9,49] Meet the conditions of the topic , And for 58 And the number of bits of each integer is 9 .
Another conditional multiple set is [19,39] .
Can prove that 2 Is the minimum length of a multiple set that satisfies the problem condition .
Example 2:
Input :num = 37, k = 2
Output :-1
explain : The number of digits is 2 The integers of cannot be added together to get 37 .
Example 3:
Input :num = 0, k = 7
Output :0
explain : The sum of empty multisets is 0 .
Tips :
0 <= num <= 3000
0 <= k <= 9
source : Power button (LeetCode)
link :https://leetcode.cn/problems/sum-of-numbers-with-units-digit-k
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
2. Problem solving
- Special circumstances should be considered first , Then consider the number of single digits from 1 - 10 individual , Can we get num The number of digits , Be careful k* Number <= num
class Solution:
def minimumNumbers(self, num: int, k: int) -> int:
if num==0:
return 0
if k==0 and num%10==0: # num = 10, k = 0
return 1
for n in range(1, 11):
if (k*n)%10 == num%10 and k*n <= num:
return n
return -1
36 ms 14.8 MB Python3
my CSDN Blog address https://michael.blog.csdn.net/
Long click or sweep code pay attention to my official account (Michael amin ), Come on together 、 Learn together !
边栏推荐
- Precautions for using timestamp type of SQLite3 database
- field.setAccessible(true);代码扫描有安全漏洞,解决方案
- redis实现分布式锁
- pm2 的学习
- 4G/wifi 能耗计量插座-监测电压电流功率
- clean,compile,build,install,package区别
- 4g/wifi energy consumption metering socket - monitoring voltage, current and power
- [解决方案] 明厨亮灶视频边缘计算网关解决方案
- What does container cloud mean? What is the difference with fortress machine?
- 消息队列之发送 Webhook 实现跨应用异步回调
猜你喜欢

lvgl使用demo示例及说明1

Want to join a big factory? Reading this article may help you

The appearance, space, safety and power are all upgraded. The xinjietu x70s will be put on the market from 87900 yuan

Error 4 opening dom ASM/Self in 0x8283c00

Special survey of moving average strategy

MySQL 8.0 新特性梳理汇总

Cancellation meeting and automatic refund processing of message queue

Pytorch learning 08: splicing and splitting

带你区分几种并行

消息队列之取消会议和自动退款处理
随机推荐
Judge whether the string type is empty and whether the list set is empty
Use of listctl virtual mode under wince
Sed technique
==And equals
[dailyfresh] course record 3 -- product search related
聚宽 - 简单策略试验
matplotlib 制作不等间距直方图
ROS 2 驱动程序现在可用于 ABB 的机械臂
Making unequal interval histogram with Matplotlib
Sending webhook of message queue to realize cross application asynchronous callback
IDEA提示 ‘Optional.get()‘ without ‘isPresent()‘ check错误。
Error 4 opening dom ASM/Self in 0x8283c00
[gstreamer] plug in writing - Test Program
Precautions for using timestamp type of SQLite3 database
Documenter l'utilisation de webcraper
Sqlite3数据库的timestamp类型的使用注意事项
03 fastjson resolving circular references
English grammar_ Adverb - loud /aloud / loud
Pytorch learning 12: automatic derivation
杨冰:OceanBase助力数字化转型,原生分布式数据库成核心系统首选