File tree 1 file changed +7
-28
lines changed
scripts/algorithms/S/Sum of Numbers With Units Digit K
1 file changed +7
-28
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 0 ms (Top 100.0%) | Memory: 7.30 MB (Top 29.79%)
2
+
1
3
class Solution {
2
4
public:
3
-
4
- // same code as that of coin change
5
- int coinChange (vector<int >& coins, int amount) {
6
- int Max = amount + 1 ;
7
- vector<int > dp (amount + 1 , INT_MAX);
8
- dp[0 ] = 0 ;
9
- for (int i = 0 ; i <= amount; i++) {
10
- for (int j = 0 ; j < coins.size (); j++) {
11
- if (coins[j] <= i && dp[i-coins[j]] !=INT_MAX) {
12
- dp[i] = min (dp[i], dp[i - coins[j]] + 1 );
13
- }
14
- }
15
- }
16
- return dp[amount] == INT_MAX ? -1 : dp[amount];
17
- }
18
-
19
-
20
-
21
- int minimumNumbers (int num, int k) {
22
- vector<int >res;
23
- for (int i = 0 ; i <= num; i++){
24
- if (i % 10 == k)
25
- res.push_back (i);
26
- }
27
- return coinChange (res, num);
28
-
29
-
5
+ int minimumNumbers (int sum, int k) {
6
+ if (sum == 0 ) return 0 ;
7
+ for (int i = 1 ; i <= 10 ; ++i)
8
+ if ((i * k) % 10 == sum % 10 && i * k <= sum) return i;
9
+ return -1 ;
30
10
}
31
-
32
11
};
You can’t perform that action at this time.
0 commit comments