Skip to content

Latest commit

 

History

History

2310

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Given two integers num and k, consider a set of positive integers with the following properties:

  • The units digit of each integer is k.
  • The sum of the integers is num.

Return the minimum possible size of such a set, or -1 if no such set exists.

Note:

  • The set can contain multiple instances of the same integer, and the sum of an empty set is considered 0.
  • The units digit of a number is the rightmost digit of the number.

 

Example 1:

Input: num = 58, k = 9
Output: 2
Explanation:
One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
Another valid set is [19,39].
It can be shown that 2 is the minimum possible size of a valid set.

Example 2:

Input: num = 37, k = 2
Output: -1
Explanation: It is not possible to obtain a sum of 37 using only integers that have a units digit of 2.

Example 3:

Input: num = 0, k = 7
Output: 0
Explanation: The sum of an empty set is considered 0.

 

Constraints:

  • 0 <= num <= 3000
  • 0 <= k <= 9

Companies: Codenation

Related Topics:
Math, Dynamic Programming, Greedy, Enumeration

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/sum-of-numbers-with-units-digit-k
// Author: github.com/lzl124631x
// Time: O(1)
// Space: O(1)
class Solution {
public:
    int minimumNumbers(int n, int k) {
        if (n == 0) return 0;
        for (int i = 1; i * k <= n && i <= 10; ++i) {
            if (i * k % 10 == n % 10) return i;
        }
        return -1;
    }
};