1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public int compute(int[] items, int n, int w) { int[] weight = new int[]{Integer.MIN_VALUE}; boolean[][] mem = new boolean[5][10]; compute(items, n, w, weight, mem, 0, 0); return weight[0]; }
private void compute2(int[] items, int n, int w, int[] weight, boolean[][] mem, int i, int cw) { if (cw == w || i == n) { if (cw > weight[0]) { weight[0] = cw; } return; }
if (mem[i][cw]) { return; }
mem[i][cw] = true;
compute2(items, n, w, weight, mem, i + 1, cw);
if (cw + items[i] <= w) { compute2(items, n, w, weight, mem, i + 1, cw + items[i]); } }
|