当前位置:网站首页>动态规划--LC474.一和零
动态规划--LC474.一和零
2022-04-21 06:52:00 【zeronose】

class Solution(object):
def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
# 这个题是一个01背包问题,不一样的是,背包的容量是2维的,有m和n
# 确定dp
# dp[i][j][k],使用前i个字符串,最多有j个0和k个1的strs的最大子集的大小为dp[i][j][k]
# i为可以选择的字符串,0<=i<=len(nums),0<=j<=m 0<=k<=n
# 状态转移方程
# 1.当j<zeros or k<ones此时不能取字符串nums[i],dp[i][j][k]=dp[i-1][j][k]
# 2.j>=zeros and k>=ones,此时可以取字符串nums[i],也可以不取
# 不取:dp[i][j][k]=dp[i-1][j][k]
# 取:dp[i][j][k]=dp[i-1][j-zeros][k-ones]+1, +1代表选取当前字符串,子集大小+1
# 初始化
dp = [[[0]*(n+1) for _ in range(m+1)] for _ in range(len(strs)+1)]
# 先遍历元素
for i in range(1, len(strs)+1):
# 选取元素后,先计算元素中0和1的数量
str_ = strs[i-1]
zeros = str_.count('0')
ones = str_.count('1')
for j in range(m+1):
for k in range(n+1):
dp[i][j][k] = dp[i-1][j][k]
if j>=zeros and k>=ones:
dp[i][j][k] = max(dp[i-1][j][k],dp[i-1][j-zeros][k-ones]+1)
return dp[-1][-1][-1]
class Solution(object):
def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
# 优化,滚动数组
dp = [[0]*(n+1) for _ in range(m+1)]
for str_ in strs:
zeros = str_.count('0')
ones = str_.count('1')
# 倒序遍历
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[-1][-1]
版权声明
本文为[zeronose]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zeronose/article/details/124303401
边栏推荐
- Assembly language -- Method of memory location
- PHP infinite classification (recursive)
- SHA256 Hashes
- leetcode 19. Delete the penultimate node of the linked list
- 343. Find the product of decomposed integers and maximize integer break
- Dynamic generation of three-level menu
- 动态生成三级菜单
- 【图像融合】基于curvelet变换实现图像融合(评价指标)含Matlab源码
- MongoDB 实验——数据备份和恢复和数据库优化
- Usage notes of Axure product prototype tool
猜你喜欢

2022-04-20: the small regiment goes to participate in the military training. The military training is coming to an end. The officer wants to divide n people in a row into m groups, and then ask each g

IIC bus design ① - IIC communication protocol

《Qt 5.12实战》简介

Gateway and distributed ID

sqlserver 两个表之间进行模糊查询,sqlserver 导入excel数据步骤

蓝牙开源协议栈BTstack之1.0 BTstack简介

结合实际聊聊防反接电路(防反接电路总结)

TypeScript函数泛型

Acrobat Pro DC 教程::如何使用文本和图片文件创建 PDF?

Renesas ide: development environment configuration during bootloader upgrade of CS + for CC
随机推荐
Bluetooth profile specification (AVRCP chapter) 5.1 connection and release of vctp
结合实际聊聊防反接电路(防反接电路总结)
【图像融合】基于curvelet变换实现图像融合(评价指标)含Matlab源码
php Rsa加密
Flutter 环境搭建等基础
Leetcode title -- 386 Dictionary rank, DFS
PHP去除字符串开头或末尾逗号
OmniPlan工具使用手册
PHP outputs all times between two specified dates
从零开始自制实现WebServer(十五)---- 日志库部分完结啦 实用小件DOUBLE-BUFFERING优化异步写入性能
Dynamically generate three-level menu
Record the problems and solutions encountered in using fastjson message converter
Unity performance optimization UI
php 输出两个指定日期中间的所有时间
联合类型和类型保护
Postgre(pg)-SQL脚本记录
Supplément à la fonction d'annotation
2022-4-20作业
Files and Directories
注解功能补充