LintCode刷题笔记-- BackpackIII

问题描述:

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?

解题思路:

又是一道恶意满满的背包问题,显然这道题我想复杂了,开始思路利用前一道题的true,false 标记的方法来解这一道题,在有恰好解的重量上进行标记,之后再对所有标记过的恰好解进行遍历找出最大的。中间还用到了面向对象的内容。显然,在方法上,这道题我想复杂了,在思路上还是没有搞清状态转移方程的真实含义。

对于这道背包问题:

  1. 从两个向量分解问题的内容,一个是拥有的背包物品数量,另外一个是背包可以拥有的最大重量。

  2. 对于每次增加一个物品,是否加入背包,需要与上一次(不加入该物品的情况下)背包总价值进行比较,如果价值更大就加入该物品,否则与上一次不加入该物品的总价值相同。

  加入这样物品在某个重量下的临界值,才会与之前没加入这项物品的情况作出比较。

  用子问题定义状态:即f[i][v]表示前 i 件物品恰放入一个容量为 j 的背包可以获得的最大价值。则其状态转移方程便是: f[i][j] = max{f[i-1][j], j>=A[i-1]? f[i-1][j-A[i-1]]+V[i-1] : 0}

参考代码:

 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     }
时间: 2024-12-11 17:10:29

LintCode刷题笔记-- BackpackIII的相关文章

LintCode刷题笔记(九章ladder PartOne)--BugFree

九章ladder的前半部分刷题笔记,在这次二刷的时候补上~ @ 2017.05.21 141 - sqrtx 二分答案 ---  binarySearch二分法 --- class Solution: """ @param x: An integer @return: The sqrt of x """ def sqrt(self, x): # write your code here if not x: return 0 start, end

LintCode刷题笔记-- LongestCommonSquence

题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. 解题思路: 这一题是非常经典的动态规划问题,在解题思路上可以按照经典的动态规划的解法,这是在系统学习动态规划之后第一个解决的LintCode上的问题: 1.子问题划分 给出两个字符串的A,B,两个字符串长度分别为lenA,lenB,求出两个字符串的LCS: 这划分为子问题:A.

LintCode刷题笔记-- Maximal Square

题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. Example For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解题思路: 1.这一题明显使用动态规划来解题,开始想法使用动态

LintCode刷题笔记-- BackpackIV

动态规划 描述: 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. Example Given nums = [1, 2, 4], target = 4 The possible combination ways are: [1, 1,

LintCode刷题笔记-- PaintHouse 1&amp;2

动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The

LintCode刷题笔记-- Maximum Product Subarray

动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 前面几道题有点儿过分依赖答案了,后面还是需要自己主动

LintCode刷题笔记-- BackpackII

标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack? Example If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we

LintCode刷题笔记-- Distinct Subsequences

题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the rel

LintCode刷题笔记-- Edit distance

描述: Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a