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 ...
随机推荐
- Django——用户认证
Django--用户认证 用户与Authentication(身份验证) Django 用户认证系统处理用户帐号,组,权限以及基于cookie的用户会话. 这个系统一般被称为 auth/auth (认 ...
- 17 IO流(十四)——Print流
PrintStream流 PrintStream作为一个包装流,它可以包装字节流,甚至可以使用指定的文件创建一个打印流.它的构造函数很丰富,建议打开API看一下. 它常用的方法是print方法与pri ...
- Linux环境下错误码及意义总结
Linux的错误码包含在/usr/include/asm-generic/errno-base.h和/usr/include/asm-generic/errno.h 这两个文件内: #ifndef _ ...
- Linux操作系统六大优点
打开百度App,看更多图片 1.免费开源.Linux是一款完全免费的操作系统,任何人都可以从网络上下载到它的源代码,并可以根据自己的需求进行定制化的开发,而且没有版权限制. 2.模块化程度高.Linu ...
- CORS讲解
跨域资源共享(CORS) 是一种机制,它使用额外的 HTTP 头来告诉浏览器 让运行在一个 origin (domain) 上的Web应用被准许访问来自不同源服务器上的指定的资源.当一个资源从与该资 ...
- 将ftp目录映射为本地盘符
1.ftpuser 下载安装FTPUSE下载地址:http://www.ferrobackup.com/ftpuse/ 创建 FTPUSE F: www.xx.com pwd /USER:admi ...
- node.js开发 npm包管理工具 npm 和 cnpm区别
npm 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用. 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用 np ...
- 解决SecureCRT 中文乱码??
在linux服务器上搭建solr,用的是SecureCRT 连接linux服务器,发现不能输入中文,配置文件中的中文也是乱码:先以为是SecureCRT工具编码的问题,把编码改成utf-8之后发现还 ...
- MFC_选择目录对话框_选择文件对话框_指定目录遍历文件
选择目录对话框 void C资源共享吧视频广告清理工具Dlg::OnBnClickedCls() { // 清空编辑框内容 m_Edit.SetWindowTextW(L""); ...
- kafka环境安装及简单使用(单机版)
一个分布式发布-订阅消息传递系统 特点: 高吞吐量.低延迟 使用场景(举例): 日志收集:用kafka收集各种服务产生的log,通过kafka以统一的接口服务的方式开放给各种consumer,如had ...