LintCode "Backpack"
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"的更多相关文章
- [LintCode] 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 ...
- 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,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
随机推荐
- android studio 中怎么使用adb无线调试
之前再eclipse下进行安卓开发,但谷歌却抛弃了eclipse而力挺android studio开发环境,没办法只好跟着走. 在eclipse下开发时调试用adb无线方式特别方便,但是在androi ...
- 十五个最常用Linux命令行 - imsoft.cnblogs
众多Linux管理员在使用Linux的时候会经常使用到很多Linux命令行,其中有绝大部分不是经常使用到的.在本文中主要为大家总结了经常使用的十五个最常用Linux命令行,希望对刚刚接触Linux命令 ...
- SSIS 组件点滴
一 Sort组件 Sort组件是用来排序,我们在做join时也必须进行排序,排序的键值作为数据源关联的key 而在sort组件中有一个选项“Remove Rows with duplicate sor ...
- jq中如何阻止元素的默认行为?
阻止网页元素的默认行为: event.preventDefault(); 或者:return false;
- jquery中prop()方法和attr()方法的区别
最近在用jquery的时候遇到一个问题,那就是attr()方法,发现这个方法有时候使用会有一些说不出原因的问题.翻翻自己之前笔记发现,还有个函数prop(). 这两个函数都可以用来获取属性. jque ...
- NOIP2016 D1T1 玩具迷題(toy)
题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...
- 关于ZF2中一点感悟,service_manager
在zf2中,在serviceLoctor中自定义的内容,可以通$serviceLocator->get('config')['key'],如果是在serivce_manger中定义的服务名,其实 ...
- console下纯字符实现的贪吃蛇
最近简直超级无聊-- code blocks win7 64编译运行无问题,应该其他编译器也不会有问题. w:上 s:下 a:左 d:右 CS标准方向控制,AK47和M4这种高级货是没有滴-- 废话不 ...
- java.util.concurrent Class ThreadPoolExecutor
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html
- DDD, MVC & Entity Framework
https://digitalpolis.co.uk/software-thoughts/ddd-mvc-entity-framework/ Data Points - Coding for Doma ...