[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,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- OpenMesh 读写网格控制(读取写入纹理坐标,法向等)
OpenMesh读取网格默认是不自动读取obj网格中的法向,纹理坐标等信息的,写入网格同样也是.所以要读取(或写入)这些信息需要修改默认的选项. 先看一下其读写网格的函数 template<cl ...
- 在64位Win7中使用Navicat Premium 和PL\SQL Developer连接Oracle数据库备忘
最近接手了一个项目,服务器端数据库是oracle 11g 64位.由于主要工作不是开发,也不想在自己的电脑上安装庞大的oracle数据库,因此寻思着只通过数据库管理工具连接数据库进行一些常用的查询操作 ...
- C# 拓展方法
/// <summary> /// 扩展类 /// </summary> public static class Extend { /// <summary> // ...
- 【转】GeoHash核心原理解析
好久没更新过博客了,先转载一篇文章吧. 源地址:http://www.cnblogs.com/LBSer/p/3310455.html 引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按 ...
- Android Manifest 权限描述大全
权限 名称 描述 android.permission.ACCESS_CHECKIN_PROPERTIES 访问登记属性 读取或写入登记check-in数据库属性表的权限 android.permis ...
- 浩瀚科技PDA移动开单|盘点机 数据采集器 条码扫描开单微POS软件 现场打印开单
PDA移动开单,是我公司的一款便携式开单配套产品,PDA能通过蓝牙.无线局域网.互联网直接与主机连接,让公司业务人员能随时随地了解公司产品信息并且进行开单.入库.库存.盘点等一系列进销存操作.是现今企 ...
- 基于netty的微服务架构
基于netty的微服务架构 微服务一篇好文章 http://san-yun.iteye.com/blog/1693759 教程 http://udn.yyuap.com/doc/essential-n ...
- stl(set+stack) LA 3634 The SetStack Computer
题目传送门 题意:给一些对集合的操作,询问每一次操作后栈顶的集合元素个数 分析:首先{}是空的,每一次add时候,{} -> { {} }变成了有一个元素的集合,利用set和stack,map容 ...
- java synchronized修饰普通方法,修饰静态方法,修饰代码块,修饰线程run方法 比较
synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得可以控制.synchronized关键字用于保护共享数据. synchronized实现同步的机制 ...
- MongoDB 入门之基础 DDL
此文章主要记录部分主要的 MongoDB 的 DDL 操作. db 查看当前所在的数据库(默认 test) > db test > show dbs 查看当前数据库服务器上的数据库名字 ...