Coin change II

Dynamic Programming DP on subsequences Hard
  • This type of problem, referred to as a variation of the "coin change" or "knapsack" problem, is commonly used in many popular software systems
  • One real-world application is in financial technology
  • For example, this algorithm could be used in ATM software to efficiently determine the smallest number of banknotes and coins to dispense that add up to a given withdrawal amount
  • Additionally, online shopping platforms could use it to suggest combinations of products that total a given amount
  • Furthermore, it's used in dynamic programming learning resources and cryptography

Give an array coins of n integers representing different coin denominations. Your task is to find the number of distinct combinations that sum up to a specified amount of money. If it's impossible to achieve the exact amount with any combination of coins, return 0.

Single coin can be used any number of times.

Return your answer with modulo 109+7.

Examples:

Input: coins = [2, 4,10], amount = 10


Output: 4


Explanation: The four combinations are:

10 = 10

10 = 4 + 4 + 2

10 = 4 + 2 + 2 + 2

10 = 2 + 2 + 2 + 2 + 2

Input: coins = [5], amount = 5


Output: 1


Explanation: There is one combination: 5 = 5.

Input: coins = [1, 2, 3, 5], amount = 5

Constraints

  • 1 <= n, coins[i], amount <= 103

Hints

  • We define dp[i] as the number of ways to achieve sum i using the given coins. the recurrence relation is: dp[i]=(dp[i]+dp[i−coin])mod(10^9+7)
  • "We iterate over coins first, ensuring that the order doesn’t affect the number of ways. We process dp[i] from left to right, ensuring each coin is considered an unlimited number of times."

Company Tags

Electronic Arts Deloitte Snowflake Cerner Instacart ARM IBM Bungie KPMG Philips Healthcare Johnson & Johnson Salesforce Morgan Stanley Zoho OYO Rooms Chewy Wayfair Airbnb Docker Epic Systems Dropbox Siemens Healthineers Texas Instruments Oracle Red Hat Google Microsoft Amazon Meta Apple Netflix Adobe