当前位置:网站首页>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;
}
};
边栏推荐
- Post infiltration flow encryption
- 734. Energy stone (greed, backpack)
- 人工智能在网络安全中的作用
- [Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
- Construction and maintenance of business websites [13]
- 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
- Modeling essays series 124 a simple coding method
- Using mongodb in laravel
- 2022 Q2 - 提升技能的技巧总结
- "C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure Exercise 3
猜你喜欢

医药管理系统(大一下C语言课设)

734. Energy stone (greed, backpack)

Selection of field types for creating tables in MySQL database

JMeter (II) - install the custom thread groups plug-in

现货黄金分析的技巧有什么呢?

【LeetCode 43】236. The nearest common ancestor of binary tree

Ks006 student achievement management system based on SSM

三分钟学会基础k线图知识

开发工具创新升级,鲲鹏推进计算产业“竹林”式生长

Have you stepped on the nine common pits in the e-commerce system?
随机推荐
机器学习基本概念
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
Exception handling of class C in yyds dry goods inventory
Logging only errors to the console Set system property ‘log4j2. debug‘ to sh
Construction and maintenance of business websites [14]
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
人工智能在网络安全中的作用
Electronic Society C language level 1 32, calculate the power of 2
Laravel artisan 常用命令
Architecture evolution from MVC to DDD
Redis有序集合如何使用
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
电子协会 C语言 1级 33 、奇偶数判断
Six lessons to be learned for the successful implementation of edge coding
Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
* and & symbols in C language
np.where 和 torch.where 用法
10 minutes to get started quickly composition API (setup syntax sugar writing method)
What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect