Backpack IV
Description
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
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 ...
随机推荐
- [转帖]龙芯3A/3B3000通用处理器出货超30万 获得“中国芯”大奖
龙芯3A/3B3000通用处理器出货超30万 获得“中国芯”大奖 http://www.eetop.cn/cpu_soc/6946247.html 2019.10 的新闻 出后量 30万 我们贡献了 ...
- RDP Error: The Identity Of The Remote Computer Cannot Be Verified
As i always need to remote to 20 servers at the same time, so i use a tool called Remote Desktop Con ...
- Java开发笔记(一百三十五)Swing的文件对话框
除了常规的提示对话框,还有一种对话框也很常见,它叫做文件对话框.文件对话框又分为两小类:打开文件的对话框.保存文件的对话框,但在Swing中它们都用类型JFileChooser来表达.下面是JFile ...
- Word 中实现公式居中编号右对齐 -- 含视频教程(9)
1. 两种方法 不管你用「Word 自带公式」还是「Mathtype」,一般来说,Word 中实现公式居中编号右对齐的方法有两种.(1):表格法:(2):制表位. 2. 方法1:表格法 >> ...
- SQL——LIKE操作符
一.LIKE操作符的基本用法 LIKE操作符用于在WHERE子句中,搜索相似.类似的数据. LIKE操作符语法: SELECT 列名1,列名2... FROM 表名 WHERE 列名 LIKE xxx ...
- JWT黑名单和白名单
单点登录系统 单点登录系统保存了用户的登录名和密码,上网用户在单点登录系统中认证成功后,就可以直接登录各个业务系统. 1. 用户使用单点登录系统的登录界面,输入用户名和密码登录成功后, 单点登录系统为 ...
- ubuntu 使用新添加的用户登录只有$解决方法
在ubuntu中,使用useradd新建的用户,默认使用的shell是dash,导致界面不美观,操作也不舒服. 情况如下: 只有美元符,不显示用户,很多乱码,且文件没有颜色. 解决方法,将该用户使用的 ...
- ①将SVN迁移到GitLab-单分支迁移
将SVN上的代码迁移到GitLab上,实际原理是将所迁移的服务器上,拷贝SVN上的相关代码,在服务器上生成Git相关仓库,然后推送到GitLab仓库,并保存SVN相关的提交记录,分支,标签等信息. 一 ...
- windows下cuda的安装
1. cuda的安装 到 https://developer.nvidia.com/cuda-toolkit 去下载.在安装的时候一定要自定义安装,否则将会安装很多无用的东西.安装的选项,可以选择不更 ...
- php生成一维码以及保存-转载
地址:http://www.cnblogs.com/ForEvErNoME/archive/2012/04/21/2460944.html 注释掉: //header('Content-Type: i ...