标签:
标签:动态规划
问题描述:
Given n items with size Ai and value Vi, and a backpack with size m. What‘s the maximum value can you put into the backpack?
1 public int backPackII(int m, int[] A, int V[]) { 2 // write your code here 3 int[][] dp = new int[A.length+1][m+1]; 4 dp[0][0] = 0; 5 for(int i=1; i<=A.length; i++){ 6 for(int j = 0; j<=m; j++){ 7 if(j<A[i-1]){ 8 dp[i][j]=dp[i-1][j]; 9 }else if(j>=A[i-1]){ 10 dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-A[i-1]]+V[i-1]); 11 } 12 13 } 14 } 15 return dp[A.length][m]; 16 }
标签:
原文地址:http://www.cnblogs.com/whaochen205/p/5847758.html