当前位置:网站首页>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;
}
};
边栏推荐
- 1069. Division of convex polygons (thinking, interval DP)
- 跨域?同源?一次搞懂什么是跨域
- Architecture evolution from MVC to DDD
- Penser au jeu 15: penser au service complet et au sous - service
- Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
- NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
- Matlab uses resample to complete resampling
- Based on configured schedule, the given trigger will never fire
- ES6 new method of string
- np. Where and torch Where usage
猜你喜欢

如何用一款产品推动「品牌的惊险一跃」?

321. Chessboard segmentation (2D interval DP)

Implementation of Weibo system based on SSM

Four basic strategies for migrating cloud computing workloads

迁移云计算工作负载的四个基本策略

Learning notes 25 - multi sensor front fusion technology

Self drawing of menu items and CListBox items

KS006基于SSM实现学生成绩管理系统

MPLS experiment operation

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
随机推荐
What are the skills of spot gold analysis?
10 minutes to get started quickly composition API (setup syntax sugar writing method)
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
Selection of field types for creating tables in MySQL database
Is the knowledge of University useless and outdated?
321. Chessboard segmentation (2D interval DP)
Introduction to ffmpeg Lib
[rust web rokcet Series 1] Hello, world and get, post, put, delete
Three core problems of concurrent programming
JMeter (I) - download, installation and plug-in management
The technology boss is ready, and the topic of position C is up to you
Matlab uses audioread and sound to read and play WAV files
Modeling essays series 124 a simple coding method
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
[Floyd] post disaster reconstruction
2022 Q2 - 提升技能的技巧总结
Openssl3.0 learning XXI provider encoder
Basic concepts of machine learning
自动浏览拼多多商品