A simple variation to 0-1 Knapsack.

class Solution {
public:
/**
* @param m: An integer m denotes the size of a backpack
* @param A: Given n items with size A[i]
* @return: The maximum size
*/
int backPack(int m, vector<int> A) {
// for i=1..N
// for v=V..0
// f[v]=max{f[v],f[v-c[i]]+w[i]};
//
size_t len = A.size();
if (len == ) return ; int ret = ;
vector<bool> dp(m + , false);
dp[] = true;
for (int i = ; i <= len; i++)
for (int v = m; v >= A[i - ]; v--)
{
if (dp[v - A[i - ]])
{
dp[v] = true;
ret = max(ret, v);
}
} return ret;
}
};

LintCode "Backpack"的更多相关文章

  1. [LintCode] Backpack VI 背包之六

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

  2. [LintCode]——目录

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

  3. leetcode & lintcode for bug-free

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

  4. leetcode & lintcode 题解

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

  5. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  6. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  7. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  8. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  9. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

随机推荐

  1. C语言Makefile文件使用

    C语言中代码Makefile文件的写法 单文件,例: #定义变量 CFLAGS=gcc #具体命令都需要一个入口,all: 这个就相当于入口,默认情况,执行第一次入口, #后面执行其他入口进行依赖,如 ...

  2. Kali 找回root 密码的操作步骤

    1. 重启kali 进入grub 界面,选择 “kali GNU/Linux, Linux 3.7-trunk-686-pae(恢复模式)” 2. 然后按下键盘E 键 3.进入编辑模式,找到Linux ...

  3. <转>windows下安装redis

    1.redis简介redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...

  4. view的加载

    这是一个listpopwindow的布局,如果listview在relativeLayout之后写的那么listview就会把relativeLayout给覆盖掉,这证明布局的加载是按照布局文件写的先 ...

  5. iOS学习笔记---c语言第八天

    指针 首先将变量a的地址存放在另一个变量中,比如存放在变量b中,然后通过变量b来间接引用变量a,间接读写变量a的值.用来存放变量地址的变量,就称为"指针变量" int *p=nul ...

  6. 用jquery ,当改变窗口或屏幕大小时调用function,用哪个事件?

    $(window).resize(function(){ //process here}); window的onresize事件. $(window).resize(function () {     ...

  7. leetcode 150. Evaluate Reverse Polish Notation ------ java

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. 顺序表及其多种实现方式 --- C/C++

    所谓顺序表,即线性表的顺序存储结构.下面给出的是数据结构---线性表的定义. ADT List{ 数据对象: 线性表的数据对象的集合为{a1,a2,a3,...,an},每个元素的类型为ElemTyp ...

  9. javascript闭包,arguments和prototype

    prototype javascript中一切皆对象,并且对象的属性和方法可以任意添加,例如: var obj=function(){}; obj.name="jack"; 但是下 ...

  10. Java 性能优化

    http://eclipsesource.com/blogs/2013/01/21/10-tips-for-using-the-eclipse-memory-analyzer/ http://docs ...