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.

Example 1
Inputcoins = [1,4,10], target = 19
Output2
We need to add coins 2 and 8, making all integers from 1 to 19 obtainable, and 2 is the minimum number of coins needed.
Example 2
Inputcoins = [1,4,10,5,7,19], target = 19
Output1
We only need to add the coin 2, making all integers from 1 to 19 obtainable, and 1 is the minimum number of coins needed.

Constraints

  • 1 <= target <= 10^5
  • 1 <= coins.length <= 10^5
  • 1 <= coins[i] <= target

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate