当前位置:网站首页>leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
leetcode2310. 个位数字为 K 的整数之和(中等,周赛)
2022-07-02 01:51:00 【重you小垃】



方法一:完全背包
在包含结尾是k的数组中,每个元素可选多次,和为num的最少个数
class Solution {
public:
int minimumNumbers(int num, int k) {
if (k % 2 == 0 && (num & 1)) return -1;
if (num == 0) return 0;
vector<int> coins;
for (int i = 1; i <= num; ++i) {
if (i % 10 == k) coins.push_back(i);
}
int n = coins.size();
vector<int> dp(num + 1, num + 1);
dp[0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = coins[i]; j <= num; ++j) {
dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
}
}
return dp[num] == num + 1 ? -1 : dp[num];
}
};
方法二:考虑个位数->数学问题
具体思路:i最少个数为1,最多为num,num只有3000,因此可以试着枚举
假设每个元素
x= 10m+k
即找到最小的i使得num=10m’+ik成立,即:遍历i,使得 (num-i k)%10==0 ,找到即return i
class Solution {
public:
int minimumNumbers(int num, int k) {
if (!num) return 0;
for (int i = 1; i <= num && num - i * k >= 0; ++i) {
if ((num - i * k) % 10 == 0) return i;
}
return -1;
}
};
边栏推荐
- Using mongodb in laravel
- Matlab uses audioread and sound to read and play WAV files
- Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
- Learn about servlets
- 跨域?同源?一次搞懂什么是跨域
- What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
- Implementation of Weibo system based on SSM
- TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
猜你喜欢

Matlab uses audioread and sound to read and play WAV files

遊戲思考15:全區全服和分區分服的思考

How to debug apps remotely and online?

The role of artificial intelligence in network security

卷積神經網絡(包含代碼與相應圖解)

Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)

Cross domain? Homology? Understand what is cross domain at once

This is the form of the K-line diagram (pithy formula)

Have you stepped on the nine common pits in the e-commerce system?
![Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation](/img/eb/b1382428d6578b8561d7fcc1a2a5cd.jpg)
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
随机推荐
正则表达式学习笔记
机器学习基本概念
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
Based on configured schedule, the given trigger will never fire
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
Is the knowledge of University useless and outdated?
Volume compression, decompression
Learning note 3 -- Key Technologies of high-precision map (Part 1)
Private project practice sharing [Yugong series] February 2022 U3D full stack class 009 unity object creation
卷積神經網絡(包含代碼與相應圖解)
卷积神经网络(包含代码与相应图解)
Brief introduction to the development of mobile network
Five skills of adding audio codec to embedded system
Learning notes 25 - multi sensor front fusion technology
Ubuntu20.04 PostgreSQL 14 installation configuration record
[rust web rokcet Series 2] connect the database and add, delete, modify and check curd
It's already 30. Can you learn programming from scratch?
Redis有序集合如何使用
人工智能在网络安全中的作用
并发编程的三大核心问题