摘要:單次選擇最大體積動規(guī)經(jīng)典題目,用數(shù)組表示書包空間為的時候能裝的物品最大容量。注意的空間要給,因為我們要求的是第個值,否則會拋出。依然是以背包空間為限制條件,所不同的是取的是價值較大值,而非體積較大值。
Backpack I Problem 單次選擇+最大體積
Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?
NoticeYou can not divide any item into small pieces.
ExampleIf 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 can fill this backpack is 10. If the backpack size is 12. we can select [2, 3, 7] so that we can fulfill the backpack.
You function should return the max size we can fill in the given backpack.
ChallengeO(n x m) time and O(m) memory.
O(n x m) memory is also acceptable if you do not know how to optimize memory.
Note動規(guī)經(jīng)典題目,用數(shù)組dp[i]表示書包空間為i的時候能裝的A物品最大容量。兩次循環(huán),外部遍歷數(shù)組A,內(nèi)部反向遍歷數(shù)組dp,若j即背包容量大于等于物品體積A[i],則取前i-1次循環(huán)求得的最大容量dp[j],和背包體積為j-A[i]時的最大容量dp[j-A[i]]與第i個物品體積A[i]之和即dp[j-A[i]]+A[i]的較大值,作為本次循環(huán)后的最大容量dp[i]。
注意dp[]的空間要給m+1,因為我們要求的是第m+1個值dp[m],否則會拋出OutOfBoundException。
Solutionpublic class Solution { public int backPack(int m, int[] A) { int[] dp = new int[m+1]; for (int i = 0; i < A.length; i++) { for (int j = m; j > 0; j--) { if (j >= A[i]) { dp[j] = Math.max(dp[j], dp[j-A[i]] + A[i]); } } } return dp[m]; } }Backpack II Problem 單次選擇+最大價值
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?
NoticeYou cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.
ExampleGiven 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.
ChallengeO(n x m) memory is acceptable, can you do it in O(m) memory?
Note和BackPack I基本一致。依然是以背包空間為限制條件,所不同的是dp[j]取的是價值較大值,而非體積較大值。所以只要把dp[j-A[i]]+A[i]換成dp[j-A[i]]+V[i]就可以了。
Solutionpublic class Solution { public int backPackII(int m, int[] A, int V[]) { int[] dp = new int[m+1]; for (int i = 0; i < A.length; i++) { for (int j = m; j > 0; j--) { if (j >= A[i]) dp[j] = Math.max(dp[j], dp[j-A[i]]+V[i]); } } return dp[m]; } }Backpack III Problem 重復(fù)選擇+最大價值
Given n kind of items with size Ai and value Vi( each item has an infinite number available) and a backpack with size m. What"s the maximum value can you put into the backpack?
NoticeYou cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.
ExampleGiven 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 15.
Solutionpublic class Solution { public int backPackIII(int[] A, int[] V, int m) { int[] dp = new int[m+1]; for (int i = 0; i < A.length; i++) { for (int j = 1; j <= m; j++) { if (j >= A[i]) dp[j] = Math.max(dp[j], dp[j-A[i]]+V[i]); } } return dp[m]; } }Backpack IV Problem 重復(fù)選擇+唯一排列+裝滿可能性總數(shù)
Given n items with size nums[i] which an integer array and all positive numbers, no duplicates. An integer target denotes the size of a backpack. Find the number of possible fill the backpack.
Each item may be chosen unlimited number of times
ExampleGiven candidate items [2,3,6,7] and target 7,
A solution set is:
[7] [2, 2, 3] return 2Solution
public class Solution { public int backPackIV(int[] nums, int target) { int[] dp = new int[target+1]; dp[0] = 1; for (int i = 0; i < nums.length; i++) { for (int j = 1; j <= target; j++) { if (nums[i] == j) dp[j]++; else if (nums[i] < j) dp[j] += dp[j-nums[i]]; } } return dp[target]; } }Backpack V Problem 單次選擇+裝滿可能性總數(shù)
Given n items with size nums[i] which an integer array and all positive numbers. An integer target denotes the size of a backpack. Find the number of possible fill the backpack.
Each item may only be used once
ExampleGiven candidate items [1,2,3,3,7] and target 7,
A solution set is:
[7] [1, 3, 3] return 2Solution
public class Solution { public int backPackV(int[] nums, int target) { int[] dp = new int[target+1]; dp[0] = 1; for (int i = 0; i < nums.length; i++) { for (int j = target; j >= 0; j--) { if (j >= nums[i]) dp[j] += dp[j-nums[i]]; } } return dp[target]; } }Backpack VI aka: Combination Sum IV Problem 重復(fù)選擇+不同排列+裝滿可能性總數(shù)
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.
NoticeThe different sequences are counted as different combinations.
ExampleGiven nums = [1, 2, 4], target = 4
The possible combination ways are:
[1, 1, 1, 1] [1, 1, 2] [1, 2, 1] [2, 1, 1] [2, 2] [4] return 6Solution
public class Solution { public int backPackVI(int[] nums, int target) { int[] dp = new int[target+1]; dp[0] = 1; for (int i = 1; i <= target; i++) { for (int num: nums) { if (num <= i) dp[i] += dp[i-num]; } } return dp[target]; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/65054.html
摘要:動規(guī)經(jīng)典題目,用數(shù)組表示書包空間為的時候能裝的物品最大容量。注意的空間要給,因為我們要求的是第個值,否則會拋出。依然是以背包空間為限制條件,所不同的是取的是價值較大值,而非體積較大值。 Backpack Problem Given n items with size Ai, an integer m denotes the size of a backpack. How full yo...
摘要:整數(shù)轉(zhuǎn)羅馬數(shù)字羅馬數(shù)字包含以下七種字符,,,,,和。字符數(shù)值例如,羅馬數(shù)字寫做,即為兩個并列的。通常情況下,羅馬數(shù)字中小的數(shù)字在大的數(shù)字的右邊。同樣地,數(shù)字表示為。給定一個整數(shù),將其轉(zhuǎn)為羅馬數(shù)字。 LeetCode12.整數(shù)轉(zhuǎn)羅馬數(shù)字 JavaScript 羅馬數(shù)字包含以下七種字符:I, V, X, L,C,D 和 M。 字符 數(shù)值 I 1 V...
摘要:本套課程包含兩大部分,第一部分是基礎(chǔ)部分,也是重要部分,參考官方文檔結(jié)構(gòu),針對內(nèi)容之間的關(guān)聯(lián)性和前后順序進行合理調(diào)整。 showImg(https://segmentfault.com/img/bVbpBA0?w=1460&h=400); 講師簡介: iview 核心開發(fā)者,iview-admin 作者,百萬級虛擬渲染表格組件 vue-bigdata-table 作者。目前就職于知名互...
摘要:和唯一的不同是組合中不能存在重復(fù)的元素,因此,在遞歸時將初始位即可。 Combination Sum I Problem Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T...
摘要:羅馬數(shù)字轉(zhuǎn)整數(shù)羅馬數(shù)字包含以下七種字符,,,,,和。字符數(shù)值例如,羅馬數(shù)字寫做,即為兩個并列的。通常情況下,羅馬數(shù)字中小的數(shù)字在大的數(shù)字的右邊。同樣地,數(shù)字表示為。給定一個羅馬數(shù)字,將其轉(zhuǎn)換成整數(shù)。 LeetCode13.羅馬數(shù)字轉(zhuǎn)整數(shù) JavaScript 羅馬數(shù)字包含以下七種字符: ·I, V, X, L,C,D 和 M。 字符 數(shù)值 I ...
閱讀 3524·2021-11-17 17:01
閱讀 3933·2021-11-08 13:12
閱讀 2487·2021-10-08 10:04
閱讀 707·2021-09-29 09:35
閱讀 1429·2021-09-26 10:12
閱讀 2055·2021-09-07 09:58
閱讀 1964·2019-08-30 15:55
閱讀 2142·2019-08-30 13:14