回溯+记忆搜索 ``` int maxValue(vector<int>&w,vector<int>&v,int capacity){ int n = w.size(); //数组存储已经得到过的dfs{i,c) vector<vector<int>> cache(n,vector<int>(capacity+1,-1)); function<int(int,int)> dfs = [&](int i,int c){ if(i < 0||c < 0) return 0; int& res = cache[i][c]; if(res!=-1) return res; if(c<w[i]){ res = dfs(i-1,c); return res; } res = max(dfs(i,c-w[i])+v[i],dfs(i-1,c)); return res; }; return dfs(n-1,capacity); } ```
点赞 评论

相关推荐

12-05 18:09
已编辑
广东药科大学 后端工程师
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务