diff --git a/Dynamic Programming/Easy/0-1 Knapsack/source.cpp b/Dynamic Programming/Easy/0-1 Knapsack/source.cpp index 5e01919..9e396ff 100644 --- a/Dynamic Programming/Easy/0-1 Knapsack/source.cpp +++ b/Dynamic Programming/Easy/0-1 Knapsack/source.cpp @@ -1,54 +1,29 @@ -#include - -#define MAX_N 100 -#define MAX_W 100 - -using namespace std; - -int N,T,W; -int weight[MAX_N],value[MAX_N]; -int memo[MAX_N][MAX_W+1]; - -int max(int x, int y){ return (x>y) ? x:y; } - -void initialize(){ - for(int i=0;i=weight[0]) ? value[0]:0; - - int result; - - if(w +using namespace std; + +int max(int a, int b) { return (a > b)? a : b; } + +int knapSack(int W, int wt[], int val[], int n) +{ + +// Base Case +if (n == 0 || W == 0) + return 0; +if (wt[n-1] > W) + return knapSack(W, wt, val, n-1); + +else return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-1), + knapSack(W, wt, val, n-1) ); +} + +// Driver code +int main() +{ + int val[] = {60, 100, 120}; + int wt[] = {10, 20, 30}; + int W = 50; + int n = sizeof(val)/sizeof(val[0]); + cout<