返回介绍

solution / 2900-2999 / 2952.Minimum Number of Coins to be Added / README_EN

发布于 2024-06-17 01:02:58 字数 5622 浏览 0 评论 0 收藏 0

2952. Minimum Number of Coins to be Added

中文文档

Description

You are given a 0-indexed integer array coins, representing the values of the coins available, and an integer target.

An integer x is obtainable if there exists a subsequence of coins that sums to x.

Return _the minimum number of coins of any value that need to be added to the array so that every integer in the range_ [1, target]_ is obtainable_.

A subsequence of an array is a new non-empty array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.

 

Example 1:

Input: coins = [1,4,10], target = 19
Output: 2
Explanation: We need to add coins 2 and 8. The resulting array will be [1,2,4,8,10].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 2 is the minimum number of coins that need to be added to the array. 

Example 2:

Input: coins = [1,4,10,5,7,19], target = 19
Output: 1
Explanation: We only need to add the coin 2. The resulting array will be [1,2,4,5,7,10,19].
It can be shown that all integers from 1 to 19 are obtainable from the resulting array, and that 1 is the minimum number of coins that need to be added to the array. 

Example 3:

Input: coins = [1,1,1], target = 20
Output: 3
Explanation: We need to add coins 4, 8, and 16. The resulting array will be [1,1,1,4,8,16].
It can be shown that all integers from 1 to 20 are obtainable from the resulting array, and that 3 is the minimum number of coins that need to be added to the array.

 

Constraints:

  • 1 <= target <= 105
  • 1 <= coins.length <= 105
  • 1 <= coins[i] <= target

Solutions

Solution 1: Greedy + Construction

Suppose the current amount we need to construct is $s$, and we have already constructed all amounts in $[0,…,s-1]$. If there is a new coin $x$, we add it to the array, which can construct all amounts in $[x, s+x-1]$.

Next, we discuss in two cases:

  • If $x \le s$, then we can merge the two intervals above to get all amounts in $[0, s+x-1]$.
  • If $x \gt s$, then we need to add a coin with a face value of $s$, so that we can construct all amounts in $[0, 2s-1]$. Then we consider the size relationship between $x$ and $s$ and continue to analyze.

Therefore, we sort the array $coins$ in ascending order, and then traverse the coins in the array from small to large. For each coin $x$, we discuss in the above two cases until $s > target$.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.

class Solution:
  def minimumAddedCoins(self, coins: List[int], target: int) -> int:
    coins.sort()
    s = 1
    ans = i = 0
    while s <= target:
      if i < len(coins) and coins[i] <= s:
        s += coins[i]
        i += 1
      else:
        s <<= 1
        ans += 1
    return ans
class Solution {
  public int minimumAddedCoins(int[] coins, int target) {
    Arrays.sort(coins);
    int ans = 0;
    for (int i = 0, s = 1; s <= target;) {
      if (i < coins.length && coins[i] <= s) {
        s += coins[i++];
      } else {
        s <<= 1;
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minimumAddedCoins(vector<int>& coins, int target) {
    sort(coins.begin(), coins.end());
    int ans = 0;
    for (int i = 0, s = 1; s <= target;) {
      if (i < coins.size() && coins[i] <= s) {
        s += coins[i++];
      } else {
        s <<= 1;
        ++ans;
      }
    }
    return ans;
  }
};
func minimumAddedCoins(coins []int, target int) (ans int) {
  slices.Sort(coins)
  for i, s := 0, 1; s <= target; {
    if i < len(coins) && coins[i] <= s {
      s += coins[i]
      i++
    } else {
      s <<= 1
      ans++
    }
  }
  return
}
function minimumAddedCoins(coins: number[], target: number): number {
  coins.sort((a, b) => a - b);
  let ans = 0;
  for (let i = 0, s = 1; s <= target; ) {
    if (i < coins.length && coins[i] <= s) {
      s += coins[i++];
    } else {
      s <<= 1;
      ++ans;
    }
  }
  return ans;
}

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

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

发布评论

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