474. Ones and Zeroes
In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.
For now, suppose you are a dominator of m 0s
and n 1s
respectively. On the other hand, there is an array with strings consisting of only 0s
and 1s
.
Now your task is to find the maximum number of strings that you can form with given m 0s
and n 1s
. Each 0
and 1
can be used at most once.
Note:
- The given numbers of
0s
and1s
will both not exceed100
- The size of given string array won't exceed
600
.
Example 1:
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
Output: 4 Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
Example 2:
Input: Array = {"10", "0", "1"}, m = 1, n = 1
Output: 2 Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
Approach #1: DP. [C++]
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> memo(m+1, vector<int>(n+1, 0));
int countOfZeros, countOfOnes;
for (auto& s : strs) {
countOfZeros = 0, countOfOnes = 0;
for (auto c : s) {
if (c == '0') countOfZeros++;
else if (c == '1') countOfOnes++;
} for (int i = m; i >= countOfZeros; --i) {
for (int j = n; j >= countOfOnes; --j) {
memo[i][j] = max(memo[i][j], memo[i-countOfZeros][j-countOfOnes] + 1);
}
}
}
return memo[m][n];
}
};
Analysis:
memo[i][j] represent the max number of strings that can be formed with i 0's and j 1's.
from the first few strings up to the current string s
Catch: have to go from bottom right to top left
If we go from top left to bottom right, we would be using results from this iteration => overcounting
Reference:
https://leetcode.com/problems/ones-and-zeroes/discuss/95814/c%2B%2B-DP-solution-with-comments
474. Ones and Zeroes的更多相关文章
- 【Leetcode】474. Ones and Zeroes
Today, Leet weekly contest was hold on time. However, i was late about 15 minutes for checking out o ...
- Week 10 - 474. Ones and Zeroes
474. Ones and Zeroes In the computer world, use restricted resource you have to generate maximum ben ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 474 Ones and Zeroes 一和零
在计算机界中,我们总是追求用有限的资源获取最大的收益.现在,假设你分别支配着 m 个 0 和 n 个 1.另外,还有一个仅包含 0 和 1 字符串的数组.你的任务是使用给定的 m 个 0 和 n 个 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- c# 将json数据转为键值对
string json = "{\"orderId\":\"000001\",\"haha\":\"001\" ...
- YARN 多租户资源池配置
简介: YARN 多租户资源池配置 当多用户同在一个 hadoop 集群作业时,就需要对资源进行有效的限制,例如区分测试.正式资源等 一.查看默认资源池 # 访问:http://192.168.1.2 ...
- 【Python爬虫】听说你又闹书荒了?豆瓣读书9.0分书籍陪你过五一
说明 五一将至,又到了学习的季节.目前流行的各大书单主打的都是豆瓣8.0评分书籍,却很少有人来聊聊这9.0评分的书籍长什么样子.刚好最近学了学python爬虫,那就拿豆瓣读书来练练手. 爬虫 本来思路 ...
- MySQL学习3---事务
MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据. 在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务. 事务处理可以用来维护数据库的完整性,保证成批的 ...
- Ubuntu使用ttyS*(如mincom)时不需root权限的方法
很久很久以前,我们在Ubuntu下使用软件(如minicom.screen等)访问串口时,是不需要任何超级权限的(使用minicom时,只有使用-s选项时需要root权限):不知道从哪个版本(12.0 ...
- zookeeper的ZAB协议
ZAB协议概述 ZooKeeper并没有完全采用Paxos算法,而是使用了一种称为ZooKeeper Atomic Broadcast(ZAB,zookeeper原子消息广播协议)的协议作为其数据一致 ...
- 关于简单的三层的简化(bll,dal,model)的封装这里全部都在一个文件主要在于明白意思
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace 封装泛型CRU ...
- BFS入门
#include<iostream> #include<cstring> #include<queue> using namespace std; #define ...
- Jenkins 邮箱配置及问题解决
Failed to send out e-mail javax.mail.MessagingException: Could not connect to SMTP host: smtp.rytong ...
- 关于:Warning: skipping non-radio button in group的处理方法整理
下面讲的是一个意思: The problem is that the next control in the tab order following the last radio button of ...