当前位置:网站首页>Leetcode knapsack problem
Leetcode knapsack problem
2022-06-22 13:21:00 【Linchong Fengxue mountain temple】

01 backpack theory
A two-dimensional dp

A one-dimensional dp

reflection
1. Why one dimension dp To flashback traversal
Because two-dimensional dp The upper and lower floors are separated from each other , It doesn't affect each other . A one-dimensional dp Influence between front and back , Only flashback traversal .
Completely backpack

Objectives and
01 knapsack




class Solution(object):
def findTargetSumWays(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
sumValue = sum(nums)
if target > sumValue or (sumValue + target) % 2 == 1 or (abs(target) > sumValue):
return 0
bagSize = (sumValue + target) // 2
dp = [0] * (bagSize + 1)
dp[0] = 1
for i in range(len(nums)):
for j in range(bagSize, nums[i] - 1, -1):
dp[j] += dp[j - nums[i]]
return dp[bagSize]474. One and zero
01 knapsack

class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
dp = [[0] * (n + 1) for _ in range(m + 1)] # Default initialization 0
# Traverse the items
for str in strs:
ones = str.count('1')
zeros = str.count('0')
# Traverse the backpack capacity and traverse from back to front !
for i in range(m, zeros - 1, -1):
for j in range(n, ones - 1, -1):
dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1)
return dp[m][n]518 Change for 2
Completely backpack

边栏推荐
- postgis 通过 st_dwithin 检索一定距离范围内的 元素
- leetcode 829. Sum of continuous integers
- 47. Permutations II
- Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
- Redis active / standby configuration dockercompose version
- 测试方法论——数据驱动测试
- leetcode 1130. 叶值的最小代价生成树
- Shell基础入门
- 241. Different Ways to Add Parentheses
- Tis tutorial 04 client
猜你喜欢

leetcode 11. 盛最多水的容器

leetcode 834. 树中距离之和

6月《中国数据库行业分析报告》发布!智能风起,列存更生

Arcpy adding layers to map documents

Redis active / standby configuration dockercompose version

MySQL 5.7 + Navicat download and installation tutorial (with installation package)

leetcode 第 297 场周赛

动作捕捉系统用于地下隧道移动机器人定位与建图

MySQL 5.7 + Navicat 下载安装教程(附安装包)

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
随机推荐
leetcode 1130. Minimum cost spanning tree of leaf value
Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
Système de classification des déchets et de gestion des transports basé sur SSM, exemple de thèse de diplôme de haute qualité (peut être utilisé directement), code source, script de base de données, t
leetcode 834. 树中距离之和
JAXB element details
240. Search a 2D Matrix II
RCE&代码执行漏洞
vs code
RobotFramework二次开发——Socket推送实时日志
leetcode 99.恢复二叉搜索树
AcWing第53场周赛
Set up your own website (5)
MySQL中的存储过程
Sap-abap- how to find a table and what related tables the fields have
Sap-abap-se14 how to recover lost data
假如,程序员面试的时候说真话
Pycharm shell script cannot be run
leetcode-区间dp
leetcode-二分法
leetcode 854. String with similarity K
https://leetcode-cn.com/problems/coin-change/solution/dai-ma-sui-xiang-lu-dai-ni-xue-tou-wan-q-80r7/
https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85.html#%E5%AE%8C%E5%85%A8%E8%83%8C%E5%8C%85