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. 「小程序JAVA实战」小程序首页视频(49)

    转自:https://idig8.com/2018/09/21/xiaochengxujavashizhanxiaochengxushouyeshipin48/ 视频显示的内容是视频的截图,用户的头像 ...

  2. StrConv 内码转换

    StrConv(string,conversion,LCID) string,预转换的字符串了(也可以使用byte数组). Conversion: 是一个整数,只决定转换方式,VB里定义了一些常量,如 ...

  3. 可视化库-Matplotlib基础设置(第三天)

    1.画一个基本的图 import numpy as np import matplotlib.pyplot as plt # 最基本的一个图,"r--" 线条加颜色, 也可以使用l ...

  4. open()打开文件失败对应的各种错误码

    open()打开文件失败错误码: 获取错误信息实例: HANDLE hFile = ; hFile = open(“c:\test.txt”, O_RDWR, S_IRWXU|S_IRWXG|S_IR ...

  5. pandas dataframe.apply() 实现对某一行/列进行处理获得一个新行/新列

    重点:dataframe.apply(function,axis)对一行或一列做出一些操作(axis=1则为对某一列进行操作,此时,apply函数每次将dataframe的一行传给function,然 ...

  6. WeakHashMap, NOT A CACHE

    Overview Base Map的实现 基于WeakReference的Entity实现 基于Reference和ReferenceQueue实现 它的弱引用是键,而不是值 它的key会被全自动回收 ...

  7. S——table

    SELECT A.LOGIN,EMAIL_ADDR,LAST_NAMEfrom S_USER AJOIN S_CONTACT B on A.ROW_ID=B.ROW_IDwhere  A.ROW_ID ...

  8. 从值栈获取List集合

    -------------------siwuxie095 从值栈获取 List 集合 1.具体步骤 (1)在 Action 中向值栈放 List 集合 (2)在 JSP 页面中从值栈获取 List ...

  9. centos7 ntp服务器配置

    一.ntp服务是什么 1. 定义 NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议. 2. 发展 首次记载在Internet Enginee ...

  10. DBA的工作职责和每日工作

    DBA一般职责 1.安装和升级数据库服务器,以及应用程序工具构建和配置网络环境. 2.熟悉数据库系统的存储结构预测未来的存储需求,制订数据库的存储方案. 3.根据开发人员设计的应用系统需求创建数据库存 ...