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. 'dict' object has no attribute 'a'

    a = {} #a.a = 'a' #AttributeError: 'dict' object has no attribute 'a' #a['a'] #KeyError: 'a' a['a'] ...

  2. MySQL的Explain解释器的部分理解

    Explain 部分说明进行解释 (1) Extra列的Using Where 表示在进行过滤后在进行Where语句的过滤 (2) type为ref,直接按索引顺序返回,没有 Using fileso ...

  3. 兼容性所有浏览器的透明CSS设置

    兼容所有浏览器的透明CSS设置: .transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; -khtml-opacity: 0. ...

  4. ZOJ 1241 Geometry Made Simple

    /*Mathematics can be so easy when you have a computer. Consider the following example. You probably ...

  5. ztong上机3

    二.实验名称:数字图像处理matlab上机 三.实验学时:2学时 四.实验目的:(详细填写) 掌握几何变换 掌握插值 理解配准的概念 五.实验内容 (1)首先自己写一个对图像进行旋转和缩放的复合变换程 ...

  6. IOS中如何获取手机的当前IP

    网上有许多类似的帖子.在搜索了资料以后.觉得下面的方法是最简单的. 使用的时候直接把类方法拖到自己新建的分类中就行. .h文件 #import <Foundation/Foundation.h& ...

  7. supervisor运行golang守护进程

    最近在鼓捣golang守护进程的实现,无意发现了supervisor这个有意思的东西.supervisor是一个unix的系统进程管理软件,可以用它来管理apache.nginx等服务,若服务挂了可以 ...

  8. eclipse中新建maven项目-转

    http://huxiaoheihei.iteye.com/blog/1766986 博客分类: maven maven  maven是个项目管理工具,集各种功能于一身,下面介绍maven web项目 ...

  9. hibernate--HQL语法与详细解释

    HQL查询: Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此 Hi ...

  10. python_redis之篇

    一.redis基本安装和简单使用 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合) ...