lintcode-medium-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.

和上一题差不多,这题里动态规划用的数组类型是int[],用来记录背包里放容积为k的物体时,这些物体的最大价值

public class Solution {
    /**
     * @param m: An integer m denotes the size of a backpack
     * @param A & V: Given n items with size A[i] and value V[i]
     * @return: The maximum value
     */
    public int backPackII(int m, int[] A, int V[]) {
        // write your code here

        if(A == null || A.length == 0)
            return 0;

        int[] dp = new int[m + 1];
        int result = 0;

        dp[0] = 0;

        for(int i = 0; i < A.length; i++){
            for(int j = m ; j >= A[i]; j--){
                dp[j] = Math.max(dp[j], dp[j - A[i]] + V[i]);
            }
        }

        for(int i = 0; i < dp.length; i++){
            if(dp[i] > result)
                result = dp[i];
        }
        return result;
    }
}
时间: 2024-12-14 23:38:23

lintcode-medium-Backpack II的相关文章

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

lintcode medium Best Time to Buy and Sell Stock I,II,III

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers with duplicate number in it. Find all unique permutations. Example Example 1: Input: [1,1] Output: [ [1,1]] Example 2: Input: [1,2,2] O

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:排颜色 II

排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 样例 给出colors=[3, 2, 2, 1, 4],k=4, 你的代码应该在原地操作使得数组变成[1, 2, 2, 3, 4] 解题 直接快排 class Solution { /** * @param colors: A list of integer * @param k: An integer * @return: nothin

[LintCode] Palindrome Partitioning II

Palindrome Partitioning II Given a string s, cut s into some substrings such that every substring is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Example For example, given s = "aab", Return 1 since the palind

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

Lintcode: k Sum II

Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. 这道题同Combination Sum II 1 public class Solution { 2 /** 3