当前位置:网站首页>leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
2022-07-02 01:54:00 【Heavy garbage】
Method 1 : Completely backpack
At the end of the inclusion is k In the array , Each element can be selected multiple times , And for num Minimum number of
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];
}
};
Method 2 : Consider single digits -> Mathematical problems
Specific ideas :i The minimum number is 1, At most num,num Only 3000, So try enumerating
Suppose each element
x= 10m+k
That is to find the smallest i bring num=10m’+ik establish , namely : Traverse i, bring (num-i k)%10==0 , Find it 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;
}
};
边栏推荐
- Is the knowledge of University useless and outdated?
- Redis环境搭建和使用的方法
- 479. Additive binary tree (interval DP on the tree)
- 并发编程的三大核心问题
- 电子协会 C语言 1级 33 、奇偶数判断
- Niuke - Huawei question bank (51~60)
- MySQL view concept, create view, view, modify view, delete view
- Using mongodb in laravel
- MPLS experiment operation
- Matlab uses resample to complete resampling
猜你喜欢
Ks006 student achievement management system based on SSM
Architecture evolution from MVC to DDD
Number of palindromes in C language (leetcode)
迁移云计算工作负载的四个基本策略
golang---锁
leetcode2312. 卖木头块(困难,周赛)
How to debug apps remotely and online?
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
Medical management system (C language course for freshmen)
随机推荐
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
Three core problems of concurrent programming
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
城市选择器组件实现原理
The role of artificial intelligence in network security
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
现货黄金分析的技巧有什么呢?
Construction and maintenance of business websites [13]
Is the knowledge of University useless and outdated?
Deep learning: a solution to over fitting in deep neural networks
Four basic strategies for migrating cloud computing workloads
【C#】使用正则校验内容
正则表达式学习笔记
Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories
2022 Q2 - résumé des compétences pour améliorer les compétences
Using mongodb in laravel
This is the form of the K-line diagram (pithy formula)
Automatically browse pinduoduo products
Self drawing of menu items and CListBox items
PR second training