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. xcrun: error: active developer path ("/XX") does not exist

    MAC OS 10.9下执行命令 svn --version 报出如下错误: xcrun: error: active developer path ("/Users/username/Do ...

  2. 深入理解Loadrunner中的Browser Emulation

    深入理解Loadrunner中的Browser Emulation 深入理解Loadrunner中的Browser Emulation 3E?']V'VgB5n*S0一:基本介绍51Testing软件 ...

  3. hdu1114 完全背包

    题意:给出钱罐的重量,然后是每种钱的价值和重量,问钱罐里最少可能有多少钱. 完全背包. 代码: #include<iostream> #include<cstdio> #inc ...

  4. 《DSP using MATLAB》示例Example5.2

    代码: L = 5; N = 20; k = [-N/2:N/2]; % square wave parameters xn = [ones(1,L), zeros(1,N-L)]; % Sq wav ...

  5. 《DSP using MATLAB》示例Example4.3 双边序列

  6. psql-03数据类型(1)

    PostgreSQL支持的数据类型远比其他数据库要多; 类型输入与转换 select int '1', date '2015-12-6'; select '1'::int, '2015-12-6':: ...

  7. Asp.net_完美设置页面最小宽度(兼容ie)

    div+css的布局相比table布局简化了前端开发的复杂性,也会带来一些问题,现在我们就说一下浮动定位在页面大小改变时布局错位的解决办法,给页面设置最小宽度: 只需更改全局css样式表 body { ...

  8. iOS 获取当前用户的用户路径并写入文件

    NSString *path = [[@"~" stringByExpandingTildeInPath] stringByAppendingString: @"/tmp ...

  9. EF框架step by step(9)—Code First Fluent API

    在上一篇中,讲述了用数据特性的方式来标识实体与数据表之间的映射关系,在Code First方法中,还可以通过Fluent API的方式来处理实体与数据表之间的映射关系. 要使用Fluent API必须 ...

  10. WPF在DLL中读取Resource的方法

    WPF是个用户控件,被WinForm调用.而WinForm是在一个DLL类库中被调用.试了很多方法,都无法将Resource中的图读进程序.用下面的方法总算实现了.   /根据图片的名称,从资源中找到 ...