92.背包问题(lintcode)
2024-09-08 04:25:08
注意j-A[i-1]必须大于等于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]
* @return: The maximum size
*/
int backPack(int m, vector<int> A) {
// write your code here
int length = A.size();
vector<vector<bool>> result(length+,vector<bool>(m+));
for(int i = ;i <= m;i++)
result[][i] = false;
for(int j = ;j <= length;j++)
result[j][] = true;
for(int i = ;i <= length;i++){
for(int j = ;j <= m;j++){
if((j-A[i-]) >= )
result[i][j] = result[i-][j-A[i-]] || result[i-][j];
else
result[i][j] = result[i-][j];
}
}
int max = ;
for(int i = ;i <= length;i++){
for(int j = ;j <= m;j++){
if(result[i][j] == true){
if(j > max)
max = j;
}
}
}
return max;
}
};
最新文章
- ref
- OC内存管理--zombie对象
- vue 单页面应用实战
- SDOI2008 Sandy的卡片( 后缀数组 )
- [bzoj3955] [WF2013]Surely You Congest
- chromedriver禁用图片,禁用js,切换UA
- bzoj2969 矩形粉刷
- Ubuntu16 编译源码安装MXNet 可变卷积Deformable-ConvNets GPU版
- React Native移植原生Android
- C# 操作Word文本框——插入表格/读取表格/删除表格
- MyCat读写分离-笔记(四)
- 解决git pull出现: Your local changes to the following files would be overwritten by merge: ...的问题
- Eclipse中的快捷键
- docker 小技巧 列出所有容器的IP地址
- [笔记]猿计划(WEB安全工程师养成之路系列教程):02HTML头部标签
- 解决centos 7.5安装openvpn,mirrors.163.com提示没有可用软件包openvpn、easy-rsa问题
- Eclipse导入jdk的源码
- C语言中关于逗号运算符的理解
- Emacs 文件中的查找操作
- KaTeX.js