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
This repository was archived by the owner on Oct 29, 2020. It is now read-only.

Latest commit

 

History

History
History
22 lines (18 loc) · 514 Bytes

File metadata and controls

22 lines (18 loc) · 514 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 Knapsack0_1 {
public static void main(String []args) {
int [] val = {60, 100, 120};
int [] wt = {10, 20, 30};
int W = 50;
int n = val.length;
System.out.println(knapsack(W, wt, val, n));
}
private static int knapsack(int W, int [] wt, int [] val, int n) {
if (W == 0 || n == 0) {
return 0;
} else if (wt[n-1] > W) {
return knapsack(W, wt, val, n-1);
} else {
return Math.max(val[n-1] + knapsack(W - wt[n-1], wt, val, n-1), knapsack(W, wt, val, n-1));
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.