What is wrong with my Code? (JAVA)

programming
interview-questions
Tags: #<Tag:0x00007f182aeb1cb8> #<Tag:0x00007f182aeb1650>

#1

public class Solution {
public int solve(int[] A, int[] B, int C) {

    // a = values
    // b = weight
    
    return knapsach(C, B, A, A.length);
}

public static int knapsach(int w, int wt[], int v[], int n) {
    int dp[][] = new int[n+1][w+1];
    
    for(int i=0;i<n;i++) {
        dp[i][0] = 0;
    }
    for(int i=0;i<w;i++) {
        dp[0][i] = 0;
    }
    
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=w;j++) {
            if(wt[i-1] <= j) {
                Math.max(dp[i-1][j], v[i-1] + dp[i-1][j - wt[i-1]]);
            } else {
                dp[i][j] = dp[i-1][j];
            }
        }
    }
    
    return dp[n][w];
}

}


#2

you’ve not assigned the Math.max() value to anything, it should be:
dp[i][j] = Math.max(p[i-1][j], v[i-1] + dp[i-1][j - wt[i-1]]);

(first if statement in the nested loop)