You have unread notifications. Click here.

Knapsack Recursion

Loading ...
Start Coding
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> w={5,5,4,7,6};
vector<int> p={4,4,3,7,3};
int maxW=10,n=5;
int solve(int i, int weight){
-if(i==n)
--return 0;
-int a = 0, b=0;
-if(w[i]+weight <= maxW)
--a = p[i] + solve(i+1, weight+w[i]);
-b = solve(i+1, weight);
-return a > b ? a : b;
}
int main(){
-cout<< solve(0,0) <<endl;
}

Exercises

English Version
Show Advanced Exercises

Advanced Exercises

Questions

Answer the following questions according to what you learned from the video.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Send

Comments & Feedback

ملحوظة يتم مراجعة التعليقات قبل ظهورها من قبل الإدارة. رجاء عدم كتابة نفس التعليق أكثر من مرة.
Back to Course