474. Ones and Zeroes
来源:互联网 发布:傅园慧表情包刷爆网络 编辑:程序博客网 时间:2024/05/20 11:24
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 = 3Output: 4Explanation: 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 = 1Output: 2Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
使用dp[i][j]表示当有i个0,j个1时,能得到的最多的字符串个数,遍历输入的数组,讨论每一个字符串是否考虑在内,递推公式:
dp[i][j]=max(dp[i][j],dp[i-zeros][j-ones]+1)
其中,zeros是当前字符串零的个数,ones是1的个数,如果将当前字符串包含在内,则dp[i][j]=dp[i-zeros][j-ones]+1;如果不讲当前字符考虑在内,总数不变。
class Solution {public: int findMaxForm(vector<string>& strs, int m, int n) { int nums=strs.size(); vector<vector<int>> res(m+1,vector<int>(n+1,0)); for(int i=0;i<nums;i++) { int zeros=0,ones=0; for(int j=0;j<strs[i].size();j++) { if(strs[i][j]=='0') zeros++; else if(strs[i][j]=='1') ones++; } for(int k=m;k>=zeros;k--) for(int l=n;l>=ones;l--) res[k][l]=max(res[k-zeros][l-ones]+1,res[k][l]); } return res[m][n]; }};
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- 474. Ones and Zeroes
- ZOJ 3963 Heap Partition( 并查集 + 贪心 +二分 )
- JS中的工厂模式
- WCF之配置文件
- Android自动化测试方法(一)
- 561. Array Partition I的C++解法
- 474. Ones and Zeroes
- IDEA第二章----配置git、tomcat(热部署)、database,让你的项目跑起来
- EXT.NET高效开发(二)——封装函数
- Java Code Examples for io.jsonwebtoken.SignatureAlgorithm
- 中企动力亮相2017年中国国际电子商务博览会
- F题 Substring(最大公共子串)
- 欢迎使用CSDN-markdown编辑器
- 快速排序
- 简单解析的几种方式: