package com.thealgorithms.dynamicprogramming;
import java.util.Arrays;
public class KnapsackMemoization {
private static int[][] t;
public static int knapsack(int[] wt, int[] value, int W, int n) {
if (t[n][W] != -1) {
return t[n][W];
}
if (n == 0 || W == 0) {
return 0;
}
if (wt[n - 1] <= W) {
t[n - 1][W - wt[n - 1]] = knapsack(wt, value, W - wt[n - 1], n - 1);
int tmp1 = value[n - 1] + t[n - 1][W - wt[n - 1]];
int tmp2 = knapsack(wt, value, W, n - 1);
t[n - 1][W] = tmp2;
int tmp = tmp1 > tmp2 ? tmp1 : tmp2;
t[n][W] = tmp;
return tmp;
} else if (wt[n - 1] > W) {
t[n][W] = knapsack(wt, value, W, n - 1);
return t[n][W];
}
return -1;
}
public static void main(String args[]) {
int[] wt = { 1, 3, 4, 5 };
int[] value = { 1, 4, 5, 7 };
int W = 10;
t = new int[wt.length + 1][W + 1];
Arrays.stream(t).forEach(a -> Arrays.fill(a, -1));
int res = knapsack(wt, value, W, wt.length);
System.out.println("Maximum knapsack value " + res);
}
}