当前位置:网站首页>Force Buckle: 474. Ones and zeros
Force Buckle: 474. Ones and zeros
2022-08-10 00:32:00 【empty__barrel】
力扣:474.一和零
题目:
给你一个二进制字符串数组 strs 和两个整数 m 和 n .
请你找出并返回 strs 的最大子集的长度,该子集中 最多 有 m 个 0 和 n 个 1 .
如果 x 的所有元素也是 y 的元素,集合 x 是集合 y 的 子集 .
dp数组含义:
dp[i][j] :0~n个物品放入容量为i个0,j个1的背包中的最大价值
递归公式:
dp[i][j] = max(dp[i][j],dp[j-current value0][j-current value1]+1)
初始化:
dp【i】【j】Either choose the original onedp【i】【j】(即初始化的dp【i】【j】)Either select the newly calculated value,The larger of the two will be chosen.
- 如果dp【i】【j】is to choose the originaldp【i】【j】即初始化的dp【i】【j】,And because at this time it is from the number of items0开始遍历,By meaning its value should be0.
- 如果dp【i】【j】The selected value is the newly calculated value,dp【i】【j】is to choose the larger value from the two,Then this is initializeddp【i】【j】cannot be greater than the newly calculated value,The newly calculated value is always greater than0.所以初始化的dp【i】【j】为0即可.
遍历顺序:
Iterate over all possibilities of the backpack weight
遍历1items into a backpack of all possible weights
遍历2items into a backpack of all possible weights
遍历nitems into a backpack of all possible weights,Only then can it be ensureddp【i】【j】选择的是dp[j-zeronum][j-onenum]+1的时候,This value is known.
Just make sure at this pointdp【i】【j】选择的是dp[j-zeronum][j-onenum]+1的时候,This value is known,那么先遍历i,j都可以.
for(string str: strs){
int onenum = 0,zeronum = 0;
for(char c: str){
if(c=='0') zeronum++;
else onenum++;
}
for(int i = m; i >= zeronum; --i){
for(int j = n; j >= onenum; --j)
{
dp[i][j] = max(dp[i][j],dp[j-zeronum][j-onenum]+1);
}
}
}
边栏推荐
猜你喜欢
完全背包理论
Bi Sheng Compiler Optimization: Lazy Code Motion
Install win7 virtual machine in Vmware and related simple knowledge
Qt message mechanism and events
集群的基础形式
The 2022-8-9 sixth group of input and output streams
2020年度SaaS TOP100企业名单
Vmware中安装win7虚拟机以及相关简单知识
Transfer Learning & Kemin Initialization
k8s部署mysql
随机推荐
OSG笔记:使用setFontResolution设置字体分辨率
ArrayList 和 LinkedList 区别
【云原生】一文讲透Kubevela addon如何添加腾讯Crane
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
iNFTnews | 迪士尼如何布局Web3
【Apifox】为什么如此受青睐,此篇文章和大家分享
kubesphere
1018.值周
深圳堡垒机厂家有哪些?重点推荐哪家?
2022-08-09 mysql/stonedb-子查询性能提升-概论
tiup cluster stop
你的手机曾经被监控过吗?
金仓数据库 KingbaseGIS 使用手册(6.3. 几何对象创建函数)
Leetcode 235. 二叉搜索树的最近公共祖先
高数_复习_第4章:向量代数和空间解析几何
Day 12 of learning to program
34. Fabric2.2 证书目录里各文件作用
新增一地公布2022下半年软考报考时间
&&、||、&、|
力扣:279.完全平方数