I am trying to learn and practice data structure and I am practicing dynamic programming, and I am stuck over one problem. I don't know if it is my logical fallacy or anything.
my problem statement is:
for a given N number of the bucket with L[i] pints of liquids in it and there is a barrel of capacity C pints, we have to find the most liquid that the barrel can be put from that bucket if the barrel should not overflow.
This is my code:
let V1 = [2, 3, 4, 2];
let barrel = 100;
const cache = [];
for(let i=0; i<=barrel; i++){
cache[i] = [];
for(let j=0; j<=V1.length; j++){
cache[i][j] = 0;
}
}
function findMaxDP(V1) {
let i, j;
let V = [0, ...V1];
for(i=0; i<=barrel; i++){
for(j=0; j<V.length; j++){
if(i==0){
cache[0][j] = 0;
}
if(j==0){
cache[i][0] = 0;
}
else{
if(i == V[j]){
cache[i][j] = V[j];
}
if((i-cache[i][j-1]) <= V[j]){
cache[i][j] = cache[i][j-1] + V[j];
}
if((i-cache[i][j-1]) != V[j]){
cache[i][j] = cache[i][j-1];
}
}
}
}
return cache[barrel-1][j-1]
}
console.log(findMaxDP(V1));
I am getting zero, any kind of help would be appreciated.
Thank you