当前位置:网站首页>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;
}
};
边栏推荐
- 如何用一款产品推动「品牌的惊险一跃」?
- Modeling essays series 124 a simple coding method
- Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
- Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
- Niuke - Huawei question bank (51~60)
- 牛客网——华为题库(51~60)
- 10 minutes to get started quickly composition API (setup syntax sugar writing method)
- The difference between new and malloc
- 1222. Password dropping (interval DP, bracket matching)
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
猜你喜欢
Discussion on the idea of platform construction
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
Exclusive delivery of secret script move disassembly (the first time)
It's already 30. Can you learn programming from scratch?
matlab 使用 resample 完成重采样
Learn about servlets
Introduction to ffmpeg Lib
Matlab uses resample to complete resampling
成功实现边缘编码需要了解的六大经验教训
随机推荐
Number of palindromes in C language (leetcode)
2022 Q2 - résumé des compétences pour améliorer les compétences
I Brief introduction of radio energy transmission technology
1222. Password dropping (interval DP, bracket matching)
Four basic strategies for migrating cloud computing workloads
现货黄金分析的技巧有什么呢?
Architecture evolution from MVC to DDD
How can the tsingsee Qingxi platform play multiple videos at the same time on the same node?
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
matlab 使用 audioread 、 sound 读取和播放 wav 文件
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
Learning note 3 -- Key Technologies of high-precision map (Part 1)
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
Parted command
人工智能在网络安全中的作用
uTools
C language 3-7 daffodils (enhanced version)
Memorabilia of domestic database in June 2022
大学的知识是否学而无用、过时?
np.where 和 torch.where 用法