这里:

for(int j = 1;j <= m;j++)
result[0][j] = 0x80000000;

不能从0开始,result[0][0]是可以取到的,是0。其他情况取不到才用最小表示。

class Solution {
public:
/*
* @param m: An integer m denotes the size of a backpack
* @param A: Given n items with size A[i]
* @param V: Given n items with value V[i]
* @return: The maximum value
*/
int backPackII(int m, vector<int> &A, vector<int> &V) {
// write your code here
int length = A.size();
vector<vector<int>> result(length+,vector<int>(m+));
for(int i = ;i <= length;i++)
result[i][] = ;
for(int j = ;j <= m;j++)
result[][j] = 0x80000000;
for(int i = ;i <= length;i++){
for(int j = ;j <= m;j++){
if((j - A[i-]) >= )
result[i][j] = max(result[i-][j-A[i-]] + V[i-],result[i-][j]);
else
result[i][j] = result[i-][j];
}
}
int max = ;
for(int i = ;i <= m;i++){
if(result[length][i] > max)
max = result[length][i];
}
return max;
}
};

背包问题2 (lintcode)的更多相关文章

  1. lintcode-125-背包问题 II

    125-背包问题 II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分. ...

  2. lintcode:背包问题II

    背包问题II 给出n个物品的体积A[i]和其价值V[i],将他们装入一个大小为m的背包,最多能装入的总价值有多大? 注意事项 A[i], V[i], n, m均为整数.你不能将物品进行切分.你所挑选的 ...

  3. lintcode:背包问题

    背包问题 在n个物品中挑选若干物品装入背包,最多能装多满?假设背包的大小为m,每个物品的大小为A[i] 样例 如果有4个物品[2, 3, 5, 7] 如果背包的大小为,可以选择的空间. 如果背包的大小 ...

  4. 92.背包问题(lintcode)

    注意j-A[i-1]必须大于等于0,只大于0会报错 class Solution { public: /** * @param m: An integer m denotes the size of ...

  5. leetcode & lintcode for bug-free

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

  6. leetcode & lintcode 题解

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

  7. lintcode 题目记录3

    Expression Expand  Word Break II Partition Equal Subset Sum  Expression Expand  字符串展开问题,按照[]前的数字展开字符 ...

  8. lintcode算法周竞赛

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

  9. [LintCode]——目录

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

随机推荐

  1. supervisor简明教程

    一.supervisor是什么 Linux的后台进程运行有好几种方法,例如nohup,screen等,但是,如果是一个服务程序,要可靠地在后台运行,我们就需要把它做成daemon,最好还能监控进程状态 ...

  2. 大白话5分钟带你走进人工智能-第二十九节集成学习之随机森林随机方式 ,out of bag data及代码(2)

              大白话5分钟带你走进人工智能-第二十九节集成学习之随机森林随机方式 ,out  of  bag  data及代码(2) 上一节中我们讲解了随机森林的基本概念,本节的话我们讲解随机森 ...

  3. 洛谷 - P4449 - 于神之怒加强版 - 莫比乌斯反演

    https://www.luogu.org/problemnew/show/P4449 \(F(n)=\sum\limits_{i=1}^{n}\sum\limits_{i=1}^{m} gcd(i, ...

  4. 15.split分割注意事项

    1.v = 'k1,v1- k2,v2- k3,v3-'变成一个字典{'k1':'v1','k2':'v2','k3:'v3'...} 变成一个字典 {'k1':'v1','k2':'v2','k3: ...

  5. Kera高层API

    目录 Keras != tf.keras Outline1 Metrics Step1.Build a meter Step2.Update data Step3.Get Average data C ...

  6. 关于idea中使用lamb表达式报错:ambda expressions are not supported at this language level

    我使用的是jdk1.8,使用lamb表达式的时候,报错 ambda expressions are not supported at this language level, 后来,设置了 接着重启了 ...

  7. B. Lecture Sleep( Educational Codeforces Round 41 (Rated for Div. 2))

    前缀后缀和搞一搞,然后枚举一下区间,找出最大值 #include <iostream> #include <algorithm> using namespace std; ; ...

  8. SpringBoot | Data Access

    https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html 配置数据源启动器.

  9. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C

    Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an aff ...

  10. Java微信公众平台开发(八)--多媒体消息回复之音乐

    我们上一篇写了关注出发图片的回复.想着在发送一次音乐,最后基于回复消息分类情况下,实现一个简单的只能话回复.先附一张大致效果图. 下面我们进入代码阶段. (一)修改消息转发器MsgDispatcher ...