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. JSON资料汇总

    网络入门学习资料 1.W3School的JSON教程:http://www.w3school.com.cn/json/index.asp 2.Introducing JSON[介绍JSON]:http ...

  2. VS2010下配置OpenMesh

    从www.openmesh.org下载最新版的安装包或者源代码,注意下载与自己系统匹配的版本,我下的是VS2010预编译版的,下载源码自己编译是一样的.安装好Visual Studio. 安装Open ...

  3. Android数据缓存(转)

    Android数据缓存   1.http://blog.csdn.net/lnb333666/article/details/8460159 2.https://github.com/Trinea/a ...

  4. 【框架】异步加载大量图片--ImageLoader

    public abstract class BaseImageLoaderProvider { public abstract void loadImage(Context ctx, ImageLoa ...

  5. 【POI word】使用POI实现对Word的读取以及生成

    项目结构如下: 那第一部分:先是读取Word文档 package com.it.WordTest; import java.io.FileInputStream; import java.io.Fil ...

  6. uva624 01背包要求输出路径

    You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...

  7. SU Demos-06Selecting Traces

    不足之处,欢迎批评指正 共3个脚本,先看readme 第1个脚本 运行结果 第2个脚本 运行结果 第3个脚本 运行结果

  8. react-redux(2)

    中间件 机制: 建立一个store.dispatch的链条,每个middleware是链条中的一个环节,传入的action对象逐步处理,直到最后出来是Javascript Plain Object; ...

  9. Inversion Sequence(csu 1555)

    Description For sequence i1, i2, i3, … , iN, we set aj to be the number of members in the sequence w ...

  10. android studio手动加入jar包

    点击启动AndroidStudio,启动后的界面如图所示. 复制你需要添加的jar,并将其黏贴到app— —src— —main— —libs文件夹下,可运行的AndroidStudio项目都有像这样 ...