Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int knapsack(int wartosci[], int wagi[], int W, int number) {
- int N = wagi.length;
- int[][] V = new int[N + 1][W + 1];
- for (int col = 0; col <= W; col++) {
- V[0][col] = 0;
- }
- for (int row = 0; row <= N; row++) {
- V[row][0] = 0;
- }
- for (int item=1;item<=N;item++)
- {
- for (int weight=1;weight<=W;weight++)
- {
- if (wagi[item-1]<=weight)
- {
- V[item][weight]=Math.max (wartosci[item-1]+V[item-1][weight-wagi[item-1]], V[item-1][weight]);
- }
- else
- {
- V[item][weight]=V[item-1][weight];
- }
- }
- }
- return V[N][W];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement