[LeetCode] Minimum Number of Coins to be Added

2952. Minimum Number of Coins to be Added

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int minimumAddedCoins(vector<int>& A, int t) {
sort(rbegin(A), rend(A));
int now = 1, res = 0;
while(now <= t) {
if(A.size() and A.back() <= now) {
now += A.back(); A.pop_back();
} else {
res += 1;
now += now;
}
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/03/PS/LeetCode/minimum-number-of-coins-to-be-added/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.