当前位置:网站首页>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

边栏推荐
- If Tiankeng majors learn IC design by themselves, will any company want it
- Sword finger offer II 114 Alien dictionary
- Acwing week 52
- 轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
- RobotFramework二次开发——文件解析
- 从零开始写一个契约测试工具
- 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
- 别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
- 6月《中国数据库行业分析报告》发布!智能风起,列存更生
- RobotFramework二次开发——Socket推送实时日志
猜你喜欢
随机推荐
文件下载漏洞&文件读取漏洞&文件删除漏洞
SAP fi financial statement version setting
leetcode 1579. 保证图可完全遍历
260. Single Number III
Acwing week 54
leetcode 829. 连续整数求和
190. Reverse Bits
Detailed installation tutorial of MySQL 8.0.29 under windows to solve the problem that vcruntime140 cannot be found_ 1.dll、plugin caching_ sha2_ password could not be loaded
In June, China database industry analysis report was released! Smart wind, train storage and regeneration
338. Counting Bits
RobotFramework二次开发——文件解析
MAUI使用Masa blazor组件库
2017年度总结
155. Min Stack
leetcode 968.监控二叉树
Windows system installs multiple MySQL versions (without uninstalling the original version), and the old and new versions are compatible.
Alicloud disk performance analysis
46. Permutations
vs code
MySQL 5.7 + Navicat 下载安装教程(附安装包)
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






