首页 > 其他分享 >LeetCode 2952. Minimum Number of Coins to be Added

LeetCode 2952. Minimum Number of Coins to be Added

时间:2024-08-22 22:49:17浏览次数:15  
标签:coins target 19 Coins Number current Added need array

原题链接在这里:https://leetcode.com/problems/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

题解:

It is a greedy question. we can start with some examples.

e.g. [1, 4, 10]. target = 19.

We can first sort the coins. For the current maximum number we can reach, initialized as 0, whenever the new coin is <= current + 1, we can extend the range.

Say current max is 0, the new coin is 1, we can extend the range to 1.

But if new coin is > current + 1, then we need to add the current max + 1.

Say current max is 1 now, new coin is 4 > 1 + 1. Thus we need to extend 1 + 1 to the range, now the range is 3. 

Do this until we reach the target.

Time Complexity: O(nlogn + log(target)). n = coins.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int minimumAddedCoins(int[] coins, int target) {
 3         int res = 0;
 4         int current = 0;
 5         int i = 0;
 6         int n = coins.length;
 7         Arrays.sort(coins);
 8         
 9         while(current < target){
10             if(i < n && coins[i] <= current + 1){
11                 current += coins[i];
12                 i++;
13             }else{
14                 current += current + 1;
15                 res++;
16             }
17         }
18 
19         return res;
20     }
21 }

 

标签:coins,target,19,Coins,Number,current,Added,need,array
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18374899

相关文章

  • P6218 [USACO06NOV] Round Numbers S 题解
    题面题目传送门如果一个正整数的二进制表示中,00的数目不小于11的数目,那么它就被称为「圆数」。例如,99的二进制表示为10011001,其中有22个00与22个11。因此,99是一个「圆数」。请你计算,区间[l,r][l,r]中有多少个「圆数」。前置芝士1.数位dp相关的题:P4317花神......
  • Fancy Coins
    感觉这个凑的题目都是分类讨论1.\(n\leqk\timesa_k\),显然先将\(a_k\)一直取到不能取为止(如果最终方案不是这样,我们可以将方案中的\(k\)个面值为\(1\)的硬币或者\(1\)个面值为\(k\)的fancycoin替换为一个面值为\(k\)的regularcoin,答案肯定不会更差),于是\(n\)%\(=k\)1).\(n\leq......
  • AGC182 C - Sum of Number of Divisors of Product
    题目大意:求长度为1,2,...N,的好序列因数个数。好序列满足每个元素\(1\leqx\leqM\)\(N\leq1e18,M\leq16\)很容易想到维护所有好序列质因数的指数+1的乘积。\(\prodb_i,1\leqi\leq6\).考虑每个数对这个乘积的影响。假设我们只有2,3,5这三个质数,序列末端添加15,......
  • c += c 与 c = c + number 的异同
    for(inti=0;i<s.length();i++){char[]chars=s.toCharArray();charc=chars[i];c=c+32;//不合法c+=32;//合法byte[]bytes=s.getBytes();byteaByte=bytes[i];......
  • K11475 丑数[Ugly Numbers,UVa136](set解法)
    题目描述丑数是指不能被2,3,5以外的其他素数整除的数。然后把丑数从小到大排列起来,前11个数如下:1,2,3,4,5,6,8,9,10,12,15,...编写一个程序,计算出第1500个丑数并输出。输入格式无输出格式输出为一行计算出的第1500个丑数替换下面句子中的‘<number>’,再输出。The1500'thuglynum......
  • Leetcode | 202 Happy Number
    分析在快乐数的题意上,通常情况下我们都会去顺着题目的意思去寻找最终结果是否为1,而后面的另一句话很有启示意义:"也可能是无限循环,但始终变不到1",那么为什么不会是无限不循环呢?证明范围限制:对于一个(d)位数的正整数(n),其最大值为99d=Max位数减少:每次计算之后,数字位数d要么减......
  • Codeforces 165E Compatible Numbers 题解
    思路高维前缀和高维前缀和把数的二进制看成一个集合,二进制的每一位为\(1\)为全集\(V\)。根据题目描述,若两数\(a,b\)相容,则\(a\operatorname{and}b=0\),容易发现,\(b\in\complement_{V}a\),所以我们只需要用高维前缀和处理出\(\complement_{V}a\)的一个元素即可。......