package org.wesome.dsalgo;
public class CoinChange {
static int coinChange(int[] coins, int finalAmountRequired) {
int[] solution = new int[finalAmountRequired + 1];
solution[0] = 1;
/* loop through all the coins */
for (int coin : coins) {
/* loop through the value of coin from 0 till final amount required */
for (int i = coin; i < (finalAmountRequired + 1); ++i) {
System.out.println("i = " + i + ", solution[i] = " + solution[i] + " + solution[i - coin] = " + solution[i - coin]);
solution[i] = solution[i] + solution[i - coin];
}
}
return solution[solution.length - 1];
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.wesome.dsalgo.CoinChange.coinChange;
public class CoinChangeTest {
@Test
void coinChangeTest() {
int[] denominations = new int[]{1, 3, 5};
int amount = 7;
int result = coinChange(denominations, amount);
System.out.print("SolveCoinChange(" + Arrays.toString(denominations) + ", " + amount + ") = ");
System.out.print(result);
}
}
plugins {
id 'java'
id "io.freefair.lombok" version "6.2.0"
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}