LintCode-BackPack II

Given n items with size A[i] and value V[i], and a backpack with size m. What‘s the maximum value can you put into the backpack?

Note

You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.

Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.

Solution:

 1 public class Solution {
 2     /**
 3      * @param m: An integer m denotes the size of a backpack
 4      * @param A & V: Given n items with size A[i] and value V[i]
 5      * @return: The maximum value
 6      */
 7     public int backPackII(int m, int[] A, int V[]) {
 8         int len = A.length;
 9         if (len==0) return -1;
10
11         int[][] maxVal = new int[len+1][m+1];
12         for (int i=0;i<=m;i++)
13             maxVal[0][i]=0;
14
15         for (int i = 1; i<=len;i++)
16             for (int s=0; s<=m; s++){
17                 maxVal[i][s] = maxVal[i-1][s];
18                 if (s>=A[i-1] && maxVal[i][s]<maxVal[i-1][s-A[i-1]]+V[i-1])
19                     maxVal[i][s] = maxVal[i-1][s-A[i-1]]+V[i-1];
20                 }
21
22         int max = 0;
23         for (int i=0;i<=m;i++)
24             if (maxVal[len][i]>max) max = maxVal[len][i];
25
26         return max;
27
28
29     }
30 }
时间: 2024-08-03 09:02:42

LintCode-BackPack II的相关文章

[LintCode] BackPack Dynamic Programming Problems

This blog talks about using dynamic programming to solve the famous back pack and its variant problems. BackPack I Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack? Notice You can not divide

Backpack II

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? Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum  value is 9. 这是最经典的0-1背

Lintcode: Backpack

Given n items with size A[i], an integer m denotes the size of a backpack. How full you can fill this backpack? Note You can not divide any item into small pieces. Example If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can sel

[LintCode] Backpack VI 背包之六

Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Notice The different sequences are counted as different combinations. Have you met this questi

LintCode : Permutations II

import java.util.Collections; class Solution { /** * @param nums: A list of integers. * @return: A list of unique permutations. */ public ArrayList<ArrayList<Integer>> permuteUnique(ArrayList<Integer> nums){ // write your code here Array

背包型动态规划

92. Backpack 可行性 https://www.lintcode.com/problem/backpack/description?_from=ladder&&fromId=16 public class Solution { /** * @param m: An integer m denotes the size of a backpack * @param A: Given n items with size A[i] * @return: The maximum size

[算法专题] 深度优先搜索&amp;回溯剪枝

1. Palindrome Partitioning https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab&

92 背包问题

原题网址:https://www.lintcode.com/problem/backpack/description 描述 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 你不可以将物品进行切割. 您在真实的面试中是否遇到过这个题?  是 样例 如果有4个物品[2, 3, 5, 7] 如果背包的大小为11,可以选择[2, 3, 5]装入背包,最多可以装满10的空间. 如果背包的大小为12,可以选择[2, 3, 7]装入背包,最多可以装满12的空间.

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

lintcode 最长上升连续子序列 II(二维最长上升连续序列)

题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 II 给定一个整数矩阵(其中,有 n 行, m 列),请找出矩阵中的最长上升连续子序列.(最长上升连续子序列可从任意行或任意列开始,向上/下/左/右任意方向移动). 样例 给定一个矩阵 [ [1 ,2 ,3 ,4 ,5], [16,17,24,23,6], [15,18,25,22,7], [14,1