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.

Have you met this question in a real interview?

Yes
Example

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 背包之六的更多相关文章

  1. LintCode "Backpack"

    A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...

  2. Backpack VI

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. 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 ...

  5. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  6. Linux学习之六——使用vi和vim

    一.vi的三种模式和相互切换 1. 一般模式 1) 移动光标 可以用箭头键,Page Up, Page Down, Home,End等按键移动光标 G,移动到档案最后一行 1G,gg,移动到档案第一行 ...

  7. LeetCode Backpack

    Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this ...

  8. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. Java中的异或(转)

    在java程序里面的异或用法: 相同输出0,不同输出1,例如: System.out.println(1^1); 输出0 System.out.println(1^2):输出3,因为最后2个低位都不一 ...

  2. ubuntu14使用qemu调试linux内核

    # 下载内核源代码编译内核 cd ~/LinuxKernel/ wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.x ...

  3. telnet时显示:允许更多到 telnet 服务器的连接。请稍候再试

    telnet时显示:允许更多到 telnet 服务器的连接.请稍候再试    解决办法: windows自带telnet服务器默认的最大连接数为2,要想修改该设置,可以在命令行键入tlntadmn c ...

  4. JMeter中的场景执行持续时间设置

    jmeter之调度器配置 JMeter的线程组设置里有一个调配器设置,用于设置该线程组下脚本执行的开始时间.结束时间.持续时间及启动延迟时间.当需要半夜执行性能测试时会用到这个功能. 设置调度器配置, ...

  5. JavaScript设计模式——前奏

    Function.prototype.method = function(name,fn){ this.prototype[name] = fn; } var Anim = function(){ / ...

  6. Uva10328 dp(递推+高精度)

    题目链接:http://vjudge.net/contest/136499#problem/F 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 一个比较好理解的题解:原题中问 ...

  7. hdu1248完全背包

    不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.死亡骑士:"我要买道具!" ...

  8. HTTP基础11--web(3)

    邮件首部注入攻击 指 Web 应用中的邮件发送功能,攻击者通过向邮件首部 To 或 Subject 内任意添加非法内容发起的攻击.利用存在安全漏洞的 Web 网站,可对任意邮件地址发送广告邮件或病毒邮 ...

  9. MFC listcontrol 分列 添加行数据 点击列头排序

    适用于 对话框程序 1.在工具箱中拖出 ListControl,然后右键-属性,view-Report 让你的ListControl变成这幅模样! 2.添加ListControl控件的control类 ...

  10. memcached启动脚本以及telnet测试

    memcached -m 1024 -u root -p 11211 -c 1024 -P /var/memcached.pid -d memcached 1.2.2 -p <num> T ...