package DynamicProgramming;import java.util.Arrays;/*** Recursive Solution for 0-1 knapsack with memoization*/public class KnapsackMemoization {private static int[][] t;// Returns the maximum value that can// be put in a knapsack of capacity Wpublic 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);// Include item in the bag. In that case add the value of the item and call for the remaining itemsint tmp1 = value[n - 1] + t[n-1][W-wt[n-1]];// Don't include the nth item in the bag anl call for remaining item without reducing the weightint tmp2 = knapsack(wt, value, W, n - 1);t[n-1][W] = tmp2;// include the larger oneint tmp = tmp1 > tmp2 ? tmp1 : tmp2;t[n][W] = tmp;return tmp;// If Weight for the item is more than the desired weight then don't include it// Call for rest of the n-1 items} else if (wt[n - 1] > W) {t[n][W] = knapsack(wt, value, W, n - 1);return t[n][W];}return -1;}// Driver codepublic 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);}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。