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 ...
随机推荐
- EL 和 JSTL
EL 什么是EL表达式 EL(Express Lanuage) 表达式可以嵌入在jsp页面内部 减少jsp脚本的编写 EL出现的目的是要替代jsp页面中脚本的编写 作用区间 EL最主要的作用是获取四大 ...
- hue database is locked
hue使用mysql作为元数据库 hue默认使用sqlite作为元数据库,不推荐在生产环境中使用.会经常出现database is lock的问题. 解决方法: 其实官网也有解决方法,不过过程似乎有点 ...
- mysql安装版卸载,解压版安装
卸载:https://blog.csdn.net/cxy_summer/article/details/70142322 解压版安装:https://blog.csdn.net/recky_wiers ...
- JSONArray的初始化的形式
1 转义字符形式 [ { \"ID\": \"1900036295\", \"DEPT\": \" ...
- R_CNN
https://blog.csdn.net/briblue/article/details/82012575 背景本篇论文的题目是 <Rich feature hierarchies for a ...
- cakephp获取最后一条sql语句
.在app\config\core.php中设置Configure::write(); .页面上追加如下代码: $dbo = ConnectionManager::getDataSource('def ...
- Ansible常用模块命令
Ansible常用模块命令 一.安装ansible yum install epel-release yum install ansible 二.配置文件配置 vi /etc/ansible/ansi ...
- java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector解决方法
java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector解决方法 错误描述:java.lang.NoClassDefFoundErro ...
- 设置手机iphone5s邮件
由于更新系统后,手机自带的邮件服务器老是报错,后来查一下,需要设置qq邮箱独立密码,http://jingyan.baidu.com/article/c146541354cefb0bfdfc4c5d. ...
- OpenGL中的像素包装理解
OpenGL中的像素包装理解 像素包装 位图和像素图很少会被紧密包装到内存中.在许多硬件平台上,考虑到性能的原因位图和像素图的每一行的数据会从特殊的字节对齐地址开始.绝大多数编译 器会自动把变量和缓冲 ...