LintCode:背包问题
来源:互联网 发布:lte中d2怎么优化 编辑:程序博客网 时间:2024/04/30 16:26
LintCode:背包问题
首先想到的是建个二维数组,动态规划,超时了。。。
class Solution: # @param m: An integer m denotes the size of a backpack # @param A: Given n items with size A[i] # @return: The maximum size def backPack(self, m, A): # write your code here n = len(A) A.insert(0, 0) ans = [[0 for i in range(m+1)] for i in range(n+1)] for i in range(1, n+1): for j in range(1, m+1): if j-A[i-1] >= 0 and j >= ans[i-1][j-A[i-1]] + A[i] and j >= A[i]: ans[i][j] = max(ans[i-1][j-A[i-1]] + A[i], ans[i-1][j], A[i]) elif j >= ans[i-1][j] and j >= A[i]: ans[i][j] = max(ans[i-1][j], A[i]) else: ans[i][j] = ans[i-1][j] return ans[n][m]
0 0
- LintCode-背包问题 II
- 【LintCode】Backpack 背包问题
- lintcode-背包问题-92
- LintCode : 背包问题 II
- LintCode :背包问题
- LintCode : 背包问题 II
- lintcode backpack 背包问题
- LintCode:背包问题
- LintCode:背包问题 II
- LintCode(M)背包问题
- LintCode:背包问题
- LintCode:01背包问题
- LintCode 62-背包问题
- LintCode 背包问题
- 背包问题-LintCode
- Lintcode 背包问题
- LintCode背包问题总结
- lintcode -- 背包问题
- 数据库
- 单例模式中的饿汉式和懒汉式(Java设计模式)
- golang年度使用总结,简洁不简单
- HDU 5769后缀数组
- SDUT3345数据结构实验之二叉树六:哈夫曼编码
- LintCode:背包问题
- 【Arduino】1.3 按键的使用进阶
- 【知识点】bit-band(stm32)
- POJ 2449 Remmarguts' Date [第k短路]
- Aizu 0189 Convenient Location 【全局最短路 floyd】
- mysql 主从原理
- iOS 开发可视化编程之Xib 简述
- C语言::模拟实现strlen函数
- Android Activity生命周期