标签:i++ 思路 back note class pos NPU find dup
Given an integer array nums[]
which contains n
unique positive numbers, num[i]
indicate the size of i
th item. An integer target
denotes the size of backpack. Find the number of ways to fill the backpack.
Each item may be chosen unlimited number of times
Example1
Input: nums = [2,3,6,7] and target = 7
Output: 2
Explanation:
Solution sets are:
[7]
[2, 2, 3]
Example2
Input: nums = [2,3,4,5] and target = 7 Output: 3 Explanation: Solution sets are: [2, 5] [3, 4] [2, 2, 3]
思路:f[i][j]表示只考虑前i件物品,取到物品重量和为j的方法数量,这题和完全背包做法类似
public class Solution { /** * @param nums: an integer array and all positive numbers, no duplicates * @param target: An integer * @return: An integer */ public int backPackIV(int[] nums, int target) { // Write your code here int m = target; int []A = nums; int f[][] = new int[A.length + 1][m + 1]; f[0][0] = 1; for (int i = 1; i <= A.length; i++) { for (int j = 0; j <= m; j++) { int k = 0; while(k * A[i-1] <= j) { f[i][j] += f[i-1][j-A[i-1]*k]; k+=1; } } // for j } // for i return f[A.length][target]; } }
标签:i++ 思路 back note class pos NPU find dup
原文地址:https://www.cnblogs.com/FLAGyuri/p/12078434.html