0-1 Knapsack Problem Dynamic Programming Solved with Code | Dynamic Programming Interview Questions

preview_player
Показать описание

NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this video, we start with the famous Knapsack problems and solve the 0-1 knapsack problem where we are required to fill a knapsack with weights up to certain capacity which have the greatest value. In this problem,

1. You are given a number n, representing the count of items.
2. You are given n numbers, representing the values of n items.
3. You are given n numbers, representing the weights of n items.
3. You are given a number "cap", which is the capacity of a bag you've.
4. You are required to calculate and print the maximum value that can be created in the bag without overflowing it's capacity.

Note1 - Each item can be taken 0 or 1 number of times. You are not allowed to put the same item again and again.

#dp #01knapsack #dynamicprogramming
Рекомендации по теме
Комментарии
Автор

I wish, we had a bigger picture like Sumit Sir has given . The relation between knapsack, target sum subset and coin change was never established . it is a lot clearer now. Now we have a technique rather than a solution to a problem. Keep up the good work !!! ..

LetsReview
Автор

i am studying from paiid course of 7k and the teacher is not able to explain clearly but he is proiving this much detailed explaininon for free from which i got it in the first try!!!!
kudos to you sir love you>3
we can bet he is the best on whole youtube without any second thoughts

treecacPie
Автор

Relating with cricket has made it a lot easier to understand the code. Thank you, sir !! 🙌

bhaveshmehta
Автор

Sir aapne aaj ka din ka bhi break nhi liya that shows how great teacher you are.
Hats off to you Sir.

shashankpal
Автор

I have suggested your channel to so many people and literally all of them loved the content . Your way of teaching is amazing sir . Thanks for explaining every concept in such an easy way .

Krishna-rtyf
Автор

i liked it very much that u gave the example of a team of 5 much more easier to understand

aryanshaw
Автор

I was able to code it in c++ after your unmatchable and awesome
Thank you so much Sumeet Sir 🌟.
God bless you 🙏

SaumyaSharma
Автор

thank you so much sir! i referred many vids on YT, but no one could explain like you did

glyoxal
Автор

Out of the world solution LOVED And I liked the fact that sumeet sir try to reply to all the comments and its rather unusual
Thank a ton sir❤

debayandebnath
Автор

Thanks a lot, Sir for this wonderful explanation.

sol-2 for same question:-

import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws Exception{
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int[]weightarr=new int[n];
int[]valuearr=new int[n];

for(int i=0;i<n;i++){
weightarr[i]=scn.nextInt();
}

for(int i=0;i<n;i++){
valuearr[i]=scn.nextInt();
}

int k=scn.nextInt(); //max target sum

//write code from here
int[][]dp=new int[n+1][k+1];

for(int i=0, j=0;i<dp.length;i++){
dp[i][j]=1;
}
for(int i=0, j=1;j<dp[0].length;j++){
dp[i][j]=0;
}

for(int i=1;i<dp.length;i++){
for(int j=1;j<dp[0].length;j++){
if(j<weightarr[i-1]){
dp[i][j]=dp[i-1][j];

}
else{

dp[i][j]=dp[i-1][j];

}
else{
int
if(val>dp[i-1][j]){
dp[i][j]=val;
}
else{
dp[i][j]=dp[i-1][j];
}
}

}
}
}
int max=1;
for(int i=dp.length-1, j=1;j<dp[0].length;j++){
if(dp[i][j]>max){
max=dp[i][j];
}


}
System.out.println(max-1);

}
}

rahulkaswan
Автор

Sumeet Sir - jitni tareef karein ham aapki, kam hai!!

AyushKumar-fklm
Автор

🙏🙏Thanks sir for such a wonderful and in-depth explanation

AYJ
Автор

I got to know about pepcoding from a comedian youtuber and I liked your videos.

AdityaPandey-ebwq
Автор

Literally amazing lecture... especially the way u related this problem with cricket made it much easier to understand.. thanks a lot sir. God bless you!

srishtikhurana
Автор

Finally I understand this concept 😭. Thank you sir

ahmadnoor
Автор

Thank you ..
The cricket example makes it a lot simpler to understand and code.

subhadeepbabai
Автор

Thank you sir it's going to be difficult for me to understand this problem without your explanation, and also it helps me to regain my confidence in DP

namangupta
Автор

Sir you are really doing great work for making dp easier for all of us 🙏.Thank you sir!!!..May your reach to many students ..Kudos to you!!! 🙏

gungunsinghal
Автор

one of the best explanation i came across 0/1 knapsack problem.lovely content.please keep making more and more videos like these.

vishaldeshpande
Автор

sir after watching your video now i understood that dp is not a nigthmare....i had started to learn dp since 4 month i was not able to grab it but now...thanku sir
sending you a loads of love and blessing❤😊💖

shivani_
join shbcf.ru