100Days of Code #Day22

Lakshmanan Subbiah
2 min readMar 12, 2021

We are going back to solving a programming question today. As we have seen before Dynamic programming involves splitting a problem into series of sub problems solving them and storing the result to avoid re computations.

Okay let’s jump on to the question,

Given an array of distinct positive integers representing coin denominations and a single non-negative integer n representing a target amount of money, write a function that returns the number of ways to make change for that target amount using the given coin denominations.

Sample Input

n=6

denoms = [1,5]

Sample Output:

2

Now, can we solve this in a normal looping method, we can but it will be taking too much of time and too much of looping. We have to find several set of combinations and check whether that will add up to 6.

Now, the dynamic programming approach, Okay where i have to split into small problems. Since we have to reach target by the denominations we have to split the target. Can we write the target as combination of denominations

6 = 5 + 1

6 = 1+ 1+1+1+1+1 (this too can be aggregated to 5+1)

Okay then Number of ways to reach 5

5 = 1+1+1+1+1 (this can be aggregated to 4+1_

5 = 5

Going on until 1 we can split the sub problem as follows for each denomination coin F(c) we can deduce the following condition

for each F(c)

if(F(i-c)>0)

F(i) = F(i)+F(i-c)

Another Example:

For each coin denomination we are checking number of ways to reach the number to which when coin added reaches target is greater than 0. If so add those possibilities to the current index’s possibilities.

#100DaysOfCode #Day22

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response