We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Define state:
dp[i][j]: # of ways to get number i, using coins values as largest as the j-th coin. (i.e. the coins chosen to construct number i cannot exceed c[j])
State transition: for each coin c[j] and fix to be the next (also largest) coin from the last step (i.e. i-c[j])
Starting point: number of ways using c[j] as the largest always >= using the second largest coin c[j-1]
dp[i][j] = dp[i][j-1]
If i no additional ways
If i==c[j]: 1 new way in addition to using at largest c[j-1], i.e. using c[j] directly to get number i
dp[i][j]+=1
If i>c[j]: first get i-c[j] in dp[i-c[j]][j] ways, then get i by adding c[j] -> dp[i-c[j]][j] new ways
dp[i][j] += dp[i-c[j]][j]
** why no duplications? for example, dp[4][] = dp[1][] + dp[3][], without [] specified, dp[1]=1 means (1+3); dp[3]=2 means (1+1+1; 3+1), where (1+3) is double-counted. If when we use a new coin, and make it the largest that can be used, then dp[4-1][1]=1 (i.e. 1+1+1).
Edge cases:
dp[i][0] = 1 if i%c[0]==0 else 0
defgetWays(n,coins):coins=sorted(coins)print(coins)dp=[[0]*len(coins)foriinrange(n+1)]foriinrange(1,n+1):print("0 : ",dp[0])# visited = {c:False for c in coins}dp[i][0]=1ifi%coins[0]==0else0forjinrange(1,len(coins)):dp[i][j]=dp[i][j-1]ifi<coins[j]:continueifi==coins[j]:dp[i][j]+=1dp[i][j]=dp[i][j]+dp[i-coins[j]][j]print(i,": ",dp[i])returndp[n][len(coins)-1]
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Coin Change Problem
You are viewing a single comment's thread. Return to all comments →
State transition: for each coin c[j] and fix to be the next (also largest) coin from the last step (i.e. i-c[j])
Edge cases: