Backpack IV
Description
Given an integer array nums[] which contains n unique positive numbers, num[i] indicate the size of ith 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
Example
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];
}
}
Backpack IV的更多相关文章
- Great Expectations
Dear friend, This game is created based on Dicken's Great Expectations. To colorful the contents, I ...
- [LintCode] Backpack VI 背包之六
Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...
- 用Kotlin开发Android应用(IV):定制视图和Android扩展
原文标题:Kotlin for Android (IV): Custom Views and Android Extensions 原文链接:http://antonioleiva.com/kotli ...
- DES带IV向量加密解密工具
链接:http://pan.baidu.com/s/1kVAV80J 密码:sgys 鉴于网上的DES加密解密都是不带IV向量的 我就自制了一个带IV向量的DES加密解密的小工具 © 2016-20 ...
- 人人都是 DBA(IV)SQL Server 内存管理
SQL Server 的内存管理是一个庞大的主题,涉及特别多的概念和技术,例如常见的 Plan Cache.Buffer Pool.Memory Clerks 等.本文仅是管中窥豹,描述常见的内存管理 ...
- leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
- 【故障处理】队列等待之enq IV - contention案例
[故障处理]队列等待之enq IV - contention案例 1.1 BLOG文档结构图 1.2 前言部分 1.2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
随机推荐
- urlencode编码 — 为什么要编码
原文链接:https://blog.csdn.net/stpeace/article/details/82892571 参考:https://blog.csdn.net/z69183787/artic ...
- flask框架(五)——支持正则写法、模板用法、请求响应、session
如果用正则的话,我们要用自定义的路由. 1导入from werkzeug.routing import BaseConverter 2我先要写一个类,然后继承BaseConverter,然后实现__i ...
- 不一样的go语言-athens源码概览
前言 上一篇文章介绍了athens私服的安装以及vgo download protocol的简要介绍.本文着重介绍go proxy sever的实现原理以及athens是如何实现的. go get ...
- mysql疑问
- subprocess.popen.kill杀死所有子进程
一.使用subprocess模块 使用subprocess模块可创建子进程. subprocess. Popen ( args , bufsize=0 , executable=None , stdi ...
- Luogu4069 SDOI2016 游戏 树链剖分、李超线段树
传送门 每一次加的是一个一次函数,一些呈一次函数的线段求最小值,显然用到李超线段树. 然后把维护序列的李超线段树强行上树,就直接套上树剖就可以了. 至于李超树如何区间查询,因为一次函数线段的最小值一定 ...
- lucene中Field简析
http://blog.csdn.net/zhaoxiao2008/article/details/14180019 先看一段lucene3代码 Document doc = new Document ...
- Oracle---视图插参数
1.创建一个参数Package create or replace package p_view_param is -- Author : ALANN -- Created : 2017/12/2 ...
- webpack+vue搭建vue项目
阅读地址: https://www.jianshu.com/p/23beadfa4aa5 源码地址:https://github.com/Ezoio/IMI-SOURCE-CODE
- String 字符串的==和eqauls区别
1.对于基本类型来说,==比较的是数据的值,equals方法也是数据的值: 对于引用类型来说,==比较的是引用的地址,equals方法比较的是对象的内容. 2.String是引用类型,用“=”创建字符 ...