Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit d6e6ae2

Browse files
authored
Update usacotools.java
1 parent 4962399 commit d6e6ae2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

usacotools.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,26 @@ public static void show2Darr(int[][] a) {
346346
public static void showarr(int[] a) {
347347
for(int x:a) {print(x+" ");}
348348
}
349+
public static int[][] dpcache;
350+
public static int ks(int W,int[] wt,int[] val,int n) {
351+
int result;
352+
if(dpcache[n][W]!=0) {return dpcache[n][W];}
353+
if(n==0||W==0) {
354+
result=0;
355+
}else if(wt[n-1]>W) {
356+
result=ks(W,wt,val,n-1);
357+
358+
359+
360+
}else {
361+
result=Math.max(val[n-1]+ks(W-wt[n-1],wt,val,n-1),ks(W,wt,val,n-1));
362+
}
363+
dpcache[n][W]=result;
364+
return result;
365+
}
366+
public static void kssetup(int n,int W) {
367+
dpcache=new int[n+1][W+1];
368+
}
349369
public static void main(String[] args) throws Exception{
350370
/*
351371
* Short demo of stuff

0 commit comments

Comments
 (0)