sweet1cris

Untitled

Jan 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. public class HW2CoinCHangingVariant {
  2.     // coins: denomination; B: total exact value; k # of coins
  3.     public boolean coinChanging(int[] coins, int B, int k) {
  4.         if (coins == null) {
  5.             return false;
  6.         }
  7.         if (B == 0) {
  8.             return true;
  9.         }
  10.         boolean[][][] c = new boolean[coins.length + 1][B + 1][k + 1];
  11.         c[0][0][0] = true;
  12.         for (int i = 0; i < c.length; i++) {
  13.             for (int b = 0; b < c[0].length; b++) {
  14.                 for (int j = 0; j < c[0][0].length; j++) {
  15.                     if (b == 0) {
  16.                         c[i][b][j] = true;
  17.                     } else if (i == 0 || j == 0) {
  18.                         c[i][b][j] = false;
  19.                     } else if (b >= coins[i - 1]) {
  20.                         c[i][b][j] = c[i - 1][b][j] || c[i - 1][b - coins[i - 1]][j - 1];
  21.                     } else {
  22.                         c[i][b][j] = c[i - 1][b][j];
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         return c[c.length - 1][c[0].length - 1][c[0][0].length - 1];
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment