474 Ones and Zeroes 一和零
在计算机界中,我们总是追求用有限的资源获取最大的收益。
现在,假设你分别支配着 m 个 0 和 n 个 1。另外,还有一个仅包含 0 和 1 字符串的数组。
你的任务是使用给定的 m 个 0 和 n 个 1 ,找到能拼出存在于数组中的字符串的最大数量。每个 0 和 1 至多被使用一次。
注意:
给定 0 和 1 的数量都不会超过 100。
给定字符串数组的长度不会超过 600。
示例 1:
输入: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
输出: 4
解释: 总共 4 个字符串可以通过 5 个 0 和 3 个 1 拼出,即 "10","0001","1","0" 。
示例 2:
输入: Array = {"10", "0", "1"}, m = 1, n = 1
输出: 2
解释: 你可以拼出 "10",但之后就没有剩余数字了。更好的选择是拼出 "0" 和 "1" 。
详见:https://leetcode.com/problems/ones-and-zeroes/description/
C++:
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n)
{
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (string str : strs)
{
int zeros = 0, ones = 0;
for (char c : str)
{
(c == '0') ? ++zeros : ++ones;
}
for (int i = m; i >= zeros; --i)
{
for (int j = n; j >= ones; --j)
{
dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1);
}
}
}
return dp[m][n];
}
};
参考:https://www.cnblogs.com/grandyang/p/6188893.html
474 Ones and Zeroes 一和零的更多相关文章
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- 【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/ ...
- [LeetCode] Preimage Size of Factorial Zeroes Function 阶乘零的原像个数函数
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- 8.Move Zeroes(移动零)
Level: Easy 题目描述: Given an array nums, write a function to move all 0's to the end of it while mai ...
- 073 Set Matrix Zeroes 矩阵置零
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...
- [LeetCode] Ones and Zeroes 一和零
In the computer world, use restricted resource you have to generate maximum benefit is what we alway ...
- [LeetCode] Set Matrix Zeroes 矩阵赋零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...
随机推荐
- Python中文问题研究
我曾经在深入浅出java中文问题系 列中研究过java的中文问题,现在中文问题已经不再羁绊我在java世界中漫游的脚步了.最近,对Python产生了浓厚的兴趣,谁知道跟中文问题这个 老朋友又一次不期而 ...
- 关于Cascading
Cascading是一个开源的Java库和应用程序编程接口(API),它为MapReduce提供了一个抽象层.它允许开发者构建出能在Hadoop集群上运行的复杂的.关键任务的数据处理应用. Casca ...
- gradle配置
一.你不想看到的 Gradle Build Running 话说在天朝当程序员也是很不容易的,不管是查阅资料还是下载东西,很多时候你会发现自己上网姿势不对,当然对大多数程序员来说,这都不是事儿.这次重 ...
- dedecms文章内页获取缩略图的调用标签
点评:文章内容页缩略图的调用,图片集内容页缩略图的调用很容易混淆,内页想调用缩略图用[filed:picname/]来实现是错误的 文章内容页缩略图的调用,图片集内容页缩略图的调用,相信大家都想找这个 ...
- JavaScript页面刷新与弹出窗口问题的解决方法
1. [代码][JavaScript]代码 一.无提示刷新网页 大家有没有发现,有些网页,刷新的时候,会弹出一个提示窗口,点“确定”才会刷新.而有的页面不会提示,不弹出提示窗口,直接就刷新了 ...
- Oracle:datafile删除后,重启server报ORA-01110
模拟实验: 创建一个表空间后,直接把数据文件删除了:然后重启server. 1. create tablespace w56 datafile '/u01/app/oracle/product/10. ...
- LA-3029(扫描线)
题意: 给定一个n*m的矩阵,一些格子是空地“F”,一些是障碍"R",找出一个全部由F组成的面积最大的子矩阵; 思路: 对每个格子维护up[i][j],le[i][j],ri[i] ...
- 【Codeforces 664A】 Complicated GCD
[题目链接] 点击打开链接 [算法] gcd(a,a+1) = 1 所以当a = b时,答案为a,否则为1 [代码] #include<bits/stdc++.h> using names ...
- PDB文件说明
文/玄魂 .PDB文件,全称为“程序数据库”文件.我们使用它(更确切的说是看到它被应用)大多数场景是调试应用程序.目前我们对.PDB文件的普遍认知是它存储了被编译文件的调试信息,作为符号文件存在.那么 ...
- maven 简单入门教学实战手册
Maven那点事儿(Eclipse版) 前言: 由于最近工作学习,总是能碰到Maven的源码.虽然平时工作并不使用Maven,但是为了学习一些源码,还是必须要了解下.这篇文章不是一个全面的Mave ...