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 0sand 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:

  1. The given numbers of 0s and 1s will both not exceed 100
  2. 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的更多相关文章

  1. 【Leetcode】474. Ones and Zeroes

    Today, Leet weekly contest was hold on time. However, i was late about 15 minutes for checking out o ...

  2. Week 10 - 474. Ones and Zeroes

    474. Ones and Zeroes In the computer world, use restricted resource you have to generate maximum ben ...

  3. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  4. 474 Ones and Zeroes 一和零

    在计算机界中,我们总是追求用有限的资源获取最大的收益.现在,假设你分别支配着 m 个 0 和 n 个 1.另外,还有一个仅包含 0 和 1 字符串的数组.你的任务是使用给定的 m 个 0 和 n 个 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. leetcode算法总结

    算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. eclipse自动生成.apt_generated、factory path

  2. C#抽象类与接口的区别【转】

    一.抽象类:      抽象类是特殊的类,只是不能被实例化(可以用派生类实例化基类对象):除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法(当然它可以有普通方法),这是普通类所不能的.抽象方 ...

  3. springboot 中 集成druid ,redis

    1,导入druid jar包 <!--引入drud--> <dependency> <groupId>com.alibaba</groupId> < ...

  4. Python_01-入门基础

    以后我会发表一系列python脚本的学习资料,python版本为2.x. 目录: 1 Python入门基础 1.1 学习资源 1.2 所有语言的入门程序---Hello World!  1.3 帮助函 ...

  5. juniper交换机配置

    1.链接串口配置: minicom -s ```bash Serial port setup  A -    Serial Device      : /dev/ttyS1               ...

  6. Jakarta Commons Cookbook

      Cookbook就是工具书,应该是前年看的,在中关村看的影印版,全英文,本书主要讲解了一下模块: Core:BeanUtils,Lang,Collections,logging Db:DbUtil ...

  7. spark VS mapreduce

    Apache Spark,一个内存数据处理的框架,现在是一个顶级Apache项目. 这是Spark迈向稳定的重要一步,因为它越来越多地在下一代大数据应用中取代MapReduce. MapReduce是 ...

  8. IndexedDB:浏览器里内置的数据库(转)

    出处;http://www.webhek.com/indexeddb/ IndexedDB是HTML5规范里新出现的浏览器里内置的数据库.对于在浏览器里存储数据,你可以使用cookies或local ...

  9. 构建搞性能可扩展asp.net网站文摘

    第1章 原则与方法 网页加载的过程: 关注感知性能,减少阻塞调用,减少往返,在所有架构层次采用缓存,优化硬盘I/O 了解浏览器的工作方式,使用ajax,silverlight和纯javascript避 ...

  10. linux每天一小步---awk命令详解

    1 命令功能 awk是linux环境下的一个强大的文本工具,由于awk天生提供对文件中文本分列进行处理,所以如果一个文件中的每行都被特定的分隔符(默认为空格)隔开,我们就可以将这个文件看成是有很多列的 ...