Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
22 lines (22 loc) · 935 Bytes

File metadata and controls

22 lines (22 loc) · 935 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class practical {
public static int getMaxValue(int weights[],int profit[],int capacity) {
int n=weights.length;//or you can use profit.length which basically means youre using how many elements are there to be choosen from
int dp[][]=new int[n+1][capacity+1];
for (int i=1;i<=n;i++) {
for(int w=0;w<=capacity;w++){
if (weights[i-1]<=w) {//include the item or exclude it
dp[i][w]=Math.max(profit[i-1]+dp[i-1][w-weights[i-1]],dp[i-1][w]);
}else{//exclude
dp[i][w]=dp[i-1][w];
}
}
}
return dp[n][capacity];
}
public static void main(String[] args) {
int[] weights = {1, 2, 3, 4};
int[] profit = {10, 15, 40, 50};
int capacity = 6;
System.out.println("Maximum value in knapsack = " + getMaxValue(weights, profit, capacity));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.