@@ -46,11 +46,9 @@ public int recursiveDP(Item[] items, int index, int remainingweight) {
46
46
}
47
47
48
48
public int recursiveDP (Item [] items , int index , int remainingweight , Map <Integer , Map <Integer , Integer >> cache ) {
49
-
50
49
if (index == items .length || items [index ] == null ) {
51
50
return 0 ;
52
51
}
53
-
54
52
if (!cache .containsKey (index )) {
55
53
cache .put (index , new HashMap <Integer , Integer >());
56
54
}
@@ -61,13 +59,12 @@ public int recursiveDP(Item[] items, int index, int remainingweight, Map<Integer
61
59
62
60
int toReturn = 0 ;
63
61
if (items [index ].weight > remainingweight ) {
64
- toReturn = naive (items , index + 1 , remainingweight );
62
+ toReturn = recursiveDP (items , index + 1 , remainingweight );
65
63
} else {
66
- toReturn = Math .max (naive (items , index + 1 , remainingweight - items [index ].weight ) + items [index ].value ,
67
- naive (items , index + 1 , remainingweight ));
64
+ toReturn = Math .max (recursiveDP (items , index + 1 , remainingweight - items [index ].weight ) + items [index ].value ,
65
+ recursiveDP (items , index + 1 , remainingweight ));
68
66
}
69
67
cacheEntry .put (remainingweight , toReturn );
70
68
return toReturn ;
71
-
72
69
}
73
70
}
0 commit comments