[LintCode] Backpack VI 背包之六
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.
Notice
The different sequences are counted as different combinations.
Given 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 6
不太懂这题名称为啥叫backpack,LeetCode上的原题,请参见我之前的博客Combination Sum IV 。
解法一:
class Solution {
public:
/**
* @param nums an integer array and all positive numbers, no duplicates
* @param target an integer
* @return an integer
*/
int backPackVI(vector<int>& nums, int target) {
vector<int> dp(target + , );
dp[] = ;
for (int i = ; i <= target; ++i) {
for (auto a : nums) {
if (a <= i) {
dp[i] += dp[i - a];
}
}
}
return dp.back();
}
};
解法二:
class Solution {
public:
/**
* @param nums an integer array and all positive numbers, no duplicates
* @param target an integer
* @return an integer
*/
int backPackVI(vector<int>& nums, int target) {
vector<int> dp(target + , );
dp[] = ;
sort(nums.begin(), nums.end());
for (int i = ; i <= target; ++i) {
for (auto a : nums) {
if (a > i) break;
dp[i] += dp[i - a];
}
}
return dp.back();
}
};
[LintCode] Backpack VI 背包之六的更多相关文章
- LintCode "Backpack"
A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...
- Backpack VI
Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- TED_Topic8:How to control someone else's arm with your brain
By Greg Gage (Neuroscientist) Greg Gage is on a mission to make brain science accessible to all. In ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- Linux学习之六——使用vi和vim
一.vi的三种模式和相互切换 1. 一般模式 1) 移动光标 可以用箭头键,Page Up, Page Down, Home,End等按键移动光标 G,移动到档案最后一行 1G,gg,移动到档案第一行 ...
- LeetCode Backpack
Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- connot resolve symbol R
出现这个原因大都是layout里的xml文件出错,就不会自动生成R文件
- LaTex学习笔记(一)
1. 语法 命令 普通命令 环境 数据 注释 2. 物理结构 导言 指定文档类型,引入宏包,定义命令,环境等 \documentclass[options]{class} \usepackage[op ...
- scrollTo , scrollBy区别
View视图中scrollTo 与scrollBy这两个函数的区别 . 首先 ,我们必须明白在Android View视图是没有边界的,Canvas是没有边界的,只不过我们通过绘制特定的View时对 ...
- Java生成验证码小工具
无意中看到一个生成简易验证码的小工具类(保存学习): 工具类代码: import java.awt.BasicStroke; import java.awt.Color; import java.aw ...
- Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据.下面POI包的下载地 ...
- 关于Char类型数据做cout输出
当用cout 或者 printf()对char 或者 unsigned char类型数据进行输出的时候,默认输出的都是字符,而不是字符对应的数值.如果要输出数值,必须做int 类型数据的强制转换. 例 ...
- java 线程演示
package unit8; public class Mainthread { public static void main(String[] args) { Thread t = new Thr ...
- Android 编程下 ListView 的 HeaderView 和 FooterView 不可选择点击
在 ListView 里,HeaderView 和 FooterView 也占一行,与其他的 item 一样,可以点击,有索引,HeaderView 的索引为0.如果要使这两项不可点击,可以使用下面的 ...
- js总结1
- Redis List命令
命令 解释 lpush key string 在key对应list的头部添加字符串元素,返回1表示成功,0表示key存在且不是list类型. rpush key string 同上,尾插入. ...