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:

  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".

思路:

参考自:http://www.cnblogs.com/grandyang/p/6188893.html

这道题是一道典型的应用DP来解的题,如果我们看到这种求总数,而不是列出所有情况的题,十有八九都是用DP来解,重中之重就是在于找出递推式。如果你第一反应没有想到用DP来做,想得是用贪心算法来做,比如先给字符串数组排个序,让长度小的字符串在前面,然后遍历每个字符串,遇到0或者1就将对应的m和n的值减小,这种方法在有的时候是不对的,比如对于{"11", "01", "10"},m=2,n=2这个例子,我们将遍历完“11”的时候,把1用完了,那么对于后面两个字符串就没法处理了,而其实正确的答案是应该组成后面两个字符串才对。所以我们需要建立一个二位的DP数组,其中dp[i][j]表示有i个0和j个1时能组成的最多字符串的个数,而对于当前遍历到的字符串,我们统计出其中0和1的个数为zeros和ones,然后dp[i - zeros][j - ones]表示当前的i和j减去zeros和ones之前能拼成字符串的个数,那么加上当前的zeros和ones就是当前dp[i][j]可以达到的个数,我们跟其原有数值对比取较大值即可,所以递推式如下:

dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1);

有了递推式,我们就可以很容易的写出代码如下:
int findMaxForm(vector<string>& strs, int m, int n)
{
vector<vector<int>> dp(m + , vector<int>(n + , ));
int ones , zeros ; for (string str : strs)
{
ones = , zeros = ;
for (char ch : str)
{
if (ch == '')zeros++;
else if (ch == '')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]+);
}
}
}
return dp[m][n];
}

[leetcode-474-Ones and Zeroes]的更多相关文章

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

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

  2. 【Leetcode】474. Ones and Zeroes

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

  3. Week 10 - 474. Ones and Zeroes

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

  4. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

  5. LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  6. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  7. Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes

    题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...

  8. LeetCode之283. Move Zeroes

    ---------------------------------------------------------------------- 解法一:空间换时间 我使用的办法也是类似于"扫描 ...

  9. 【leetcode】Factorial Trailing Zeroes

    题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...

  10. ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

随机推荐

  1. 微信公众号开发《三》微信JS-SDK之地理位置的获取,集成百度地图实现在线地图搜索

    本次讲解微信开发第三篇:获取用户地址位置信息,是非常常用的功能,特别是服务行业公众号,尤为需要该功能,本次讲解的就是如何调用微信JS-SDK接口,获取用户位置信息,并结合百度地铁,实现在线地图搜索,与 ...

  2. 【转】Java方向如何准备BAT技术面试答案(汇总版)

    原文地址:http://www.jianshu.com/p/1f1d3193d9e3 这个主题的内容之前分三个篇幅分享过,导致网络上传播的比较分散,所以本篇做了一个汇总,同时对部分内容及答案做了修改, ...

  3. [开源] 基于ABP,Hangfire的开源Sharepoint文件同步程序----SuperRocket.SPSync

    (一)项目背景 Sharepoint是微软的一个产品,很多公司都在使用它,也有很多公司以前使用它,现在可能需要移植到别的平台,也可能只是移植其中的文件存储,比如说移植到微软云,或者亚马逊云存储.Sup ...

  4. 【持续集成】GIT+jenkins+snoar——jenkins发布php、maven项目

    一.持续集成 1.1 什么是持续集成? continuous integration (CI),持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员,每天至少集成一次,也就意味着 ...

  5. js实现超出一定字数隐藏并用省略号"..."代替,点击后又可进行展开和收起,

    原理简单阐述:放两个一模一样的div,把你要展示的文字放进去.页面初始化的时候,第一个div展示,第二个 div隐藏,就是这么简单.(ps:可以直接复制代码到你自己项目中,查看效果) 样式部分(记得引 ...

  6. tornado之文件上传的几种形式form,伪ajax(iframe)

    1直接form提交给后台处理 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  7. python——爬虫&问题解决&思考(三)

    继续上一篇文章的内容,上一篇文章中,将爬虫调度器已经写好了,调度器是整个爬虫程序的"大脑",也可以称之为指挥中心.而现在,我们要做的就是去将调度器中用到的其他组件写好.首先是url ...

  8. 手把手教你用Eclipse+TestNG搭建接口自动化测试框架

    转载于:http://qa.blog.163.com/blog/static/190147002201510275306185/ 把群博里关于接口自动化的文章都看了一遍,都是关于测试过程中遇到的问题及 ...

  9. javaSE_06Java中的数组(array)-思维导图

    思维导图看不清楚时: 1)可以将图片另存为图片,保存在本地来查看 : 2)右击在新标签中打开放大查看 (IE不支持,搜狗,360可以):

  10. .net—— webservice的新建、发布、使用(最全、最简单)【原创】

    网上有很多关于webservice资料,但大部分都不完整,其中还要很大部分是转载的--.这个悲剧了,自己都没试过能不能用就不负责任的转载. 所以今天对webservice的新建.发布.使用最一个全面. ...