Minimum Number of Coins to be Added
Problem
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 theminimum number of coins of any value that need to be added to the array so that every integer in the range [1, target]isobtainable.
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.
Examples
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 <= 10^51 <= coins.length <= 10^51 <= coins[i] <= target
Solution
Method 1 – Greedy Coverage
Intuition
To cover every sum from 1 to target, always add the smallest value that cannot be formed yet. This greedy approach ensures minimal additions, as each new coin extends the range of obtainable sums maximally.
Approach
- Sort the coins array.
- Initialize
miss = 1(smallest sum not yet covered) andadded = 0(coins added). - Iterate through coins:
- If coin ≤ miss, add coin to coverage (
miss += coin). - Else, add a coin of value
miss(miss += miss,added++).
- If coin ≤ miss, add coin to coverage (
- Continue until
miss > target. - Return
added.
Code
C++
class Solution {
public:
int minimumAddedCoins(vector<int>& coins, int target) {
sort(coins.begin(), coins.end());
long miss = 1, i = 0, added = 0;
while (miss <= target) {
if (i < coins.size() && coins[i] <= miss) {
miss += coins[i++];
} else {
miss += miss;
added++;
}
}
return added;
}
};
Go
func minimumAddedCoins(coins []int, target int) int {
sort.Ints(coins)
miss, i, added := 1, 0, 0
for miss <= target {
if i < len(coins) && coins[i] <= miss {
miss += coins[i]
i++
} else {
miss += miss
added++
}
}
return added
}
Java
class Solution {
public int minimumAddedCoins(int[] coins, int target) {
Arrays.sort(coins);
long miss = 1;
int i = 0, added = 0;
while (miss <= target) {
if (i < coins.length && coins[i] <= miss) {
miss += coins[i++];
} else {
miss += miss;
added++;
}
}
return added;
}
}
Kotlin
class Solution {
fun minimumAddedCoins(coins: IntArray, target: Int): Int {
coins.sort()
var miss = 1L
var i = 0
var added = 0
while (miss <= target) {
if (i < coins.size && coins[i] <= miss) {
miss += coins[i]
i++
} else {
miss += miss
added++
}
}
return added
}
}
Python
class Solution:
def minimumAddedCoins(self, coins: list[int], target: int) -> int:
coins.sort()
miss: int = 1
i: int = 0
added: int = 0
while miss <= target:
if i < len(coins) and coins[i] <= miss:
miss += coins[i]
i += 1
else:
miss += miss
added += 1
return added
Rust
impl Solution {
pub fn minimum_added_coins(mut coins: Vec<i32>, target: i32) -> i32 {
coins.sort();
let mut miss = 1;
let mut i = 0;
let mut added = 0;
while miss <= target {
if i < coins.len() && coins[i] <= miss {
miss += coins[i];
i += 1;
} else {
miss += miss;
added += 1;
}
}
added
}
}
TypeScript
class Solution {
minimumAddedCoins(coins: number[], target: number): number {
coins.sort((a, b) => a - b);
let miss = 1, i = 0, added = 0;
while (miss <= target) {
if (i < coins.length && coins[i] <= miss) {
miss += coins[i];
i++;
} else {
miss += miss;
added++;
}
}
return added;
}
}
Complexity
- ⏰ Time complexity:
O(n log n), for sorting and greedy iteration. - 🧺 Space complexity:
O(1), only a few variables are used.