返回介绍

solution / 2300-2399 / 2310.Sum of Numbers With Units Digit K / README_EN

发布于 2024-06-17 01:03:07 字数 5297 浏览 0 评论 0 收藏 0

2310. Sum of Numbers With Units Digit K

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def minimumNumbers(self, num: int, k: int) -> int:
    if num == 0:
      return 0
    for i in range(1, num + 1):
      if (t := num - k * i) >= 0 and t % 10 == 0:
        return i
    return -1
class Solution {
  public int minimumNumbers(int num, int k) {
    if (num == 0) {
      return 0;
    }
    for (int i = 1; i <= num; ++i) {
      int t = num - k * i;
      if (t >= 0 && t % 10 == 0) {
        return i;
      }
    }
    return -1;
  }
}
class Solution {
public:
  int minimumNumbers(int num, int k) {
    if (num == 0) return 0;
    for (int i = 1; i <= num; ++i) {
      int t = num - k * i;
      if (t >= 0 && t % 10 == 0) return i;
    }
    return -1;
  }
};
func minimumNumbers(num int, k int) int {
  if num == 0 {
    return 0
  }
  for i := 1; i <= num; i++ {
    t := num - k*i
    if t >= 0 && t%10 == 0 {
      return i
    }
  }
  return -1
}
function minimumNumbers(num: number, k: number): number {
  if (!num) return 0;
  let digit = num % 10;
  for (let i = 1; i < 11; i++) {
    let target = i * k;
    if (target <= num && target % 10 == digit) return i;
  }
  return -1;
}

Solution 2

class Solution:
  def minimumNumbers(self, num: int, k: int) -> int:
    if num == 0:
      return 0
    for i in range(1, 11):
      if (k * i) % 10 == num % 10 and k * i <= num:
        return i
    return -1
class Solution {
  public int minimumNumbers(int num, int k) {
    if (num == 0) {
      return 0;
    }
    for (int i = 1; i <= 10; ++i) {
      if ((k * i) % 10 == num % 10 && k * i <= num) {
        return i;
      }
    }
    return -1;
  }
}
class Solution {
public:
  int minimumNumbers(int num, int k) {
    if (!num) return 0;
    for (int i = 1; i <= 10; ++i)
      if ((k * i) % 10 == num % 10 && k * i <= num)
        return i;
    return -1;
  }
};
func minimumNumbers(num int, k int) int {
  if num == 0 {
    return 0
  }
  for i := 1; i <= 10; i++ {
    if (k*i)%10 == num%10 && k*i <= num {
      return i
    }
  }
  return -1
}

Solution 3

class Solution:
  def minimumNumbers(self, num: int, k: int) -> int:
    @cache
    def dfs(v):
      if v == 0:
        return 0
      if v < 10 and v % k:
        return inf
      i = 0
      t = inf
      while (x := i * 10 + k) <= v:
        t = min(t, dfs(v - x))
        i += 1
      return t + 1

    if num == 0:
      return 0
    if k == 0:
      return -1 if num % 10 else 1
    ans = dfs(num)
    return -1 if ans >= inf else ans

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文