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

  It seems like every thing gone well. First problem was accepted by my first try. Second problem is not a complex task but has many coding work to solve it.

  What makes me feel fool is the third problem which is absolutely  a dp problem using two dimensions array. I was solved it by greedy. WTF, the reason of that I think may be I look down upon this problem.

I hope to solve it in easy stpes but I didn't think over it's weakness.

  

474. Ones and Zeroes

 
 
 
  • User Accepted: 171
  • User Tried: 359
  • Total Accepted: 175
  • Total Submissions: 974
  • Difficulty: Medium

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".
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>>dp(m + , vector<int>(n + , ));
int len = strs.size();
for(int i = ; i < len; i ++){
int z = , o = ;
for(auto &ch : strs[i]){
if(ch == '') z ++;
else o ++;
}
for(int j = m; j >= z; j --){
for(int k = n; k >= o; k --){
dp[j][k] = max(dp[j][k], dp[j - z][k - o] + );
}
}
}
int ret = ;
for(int i = ; i <= m; i ++)
for(int j = ; j <= n; j ++)
ret = max(ret, dp[i][j]);
return ret;
}
};

【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】172. Factorial Trailing Zeroes

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...

  3. 【LeetCode】73. Set Matrix Zeroes (2 solutions)

    Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...

  4. 【LeetCode】-- 73. Set Matrix Zeroes

    问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...

  5. 【LeetCode】172. Factorial Trailing Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 递归 循环 日期 题目描述 Given an integer ...

  6. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

  7. 【LeetCode】73. 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. Fo ...

  8. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. 洛谷 2055 BZOJ 1433 [ZJOI2009]假期的宿舍

    [题解] 既然是一人对应一床,那么显然可以用二分图匹配来做.俩人认识的话,如果其中一个a是在校学生,另一个b不回家,b就可以向a的床连边(a,b当然也可以是同一个人). 然后如果最大匹配数大于等于需要 ...

  2. c#string类型反序列化成字典类型

    c# 实现string类型转化为字典类型:黄色底纹为需要引用的dll,可以在网站下载! 下载地址:http://download.csdn.net/download/xinping_168/47107 ...

  3. [转] angular2+highcharts+chart.js

    这里是在ionic2下使用highchairs和chart.js的简单示例chartjs部分参考http://www.jianshu.com/p/bc18132da812 1.安装hightchart ...

  4. idea 背景颜色设置

    1. 设置当前鼠标所在行颜色 2. 设置编辑区颜色

  5. BNUOJ 13358 Binary Apple Tree

    Binary Apple Tree Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Orig ...

  6. HDU 2082 母函数法

    #include <cstdio> #include <cstring> using namespace std; ] , dp[][]; int main() { // fr ...

  7. hdu 5040bfs+优先队列 需要存状态

    /* 剪枝:四秒后状态会变得和原来一样,所以四秒后如果再经过这个点肯定不是最优的舍去 易错点:在一个是从.到.这两个点都没有被照到并且不是摄像机,也可能需要等3秒,因为后面的结果可能再这三秒中发生改变 ...

  8. kendo grid 点击新增没有反映

    在datasource中缺少 editable: "inline",这一行

  9. Codeforces Round #391(div 1+2)

    A =w= B QuQ C 题意:有n个体育场,每个体育场有一些小精灵,一共m种小精灵(n<=1e5,m<=1e6),可以将数字全为i的精灵进化成j(可以互相进化也可以选择不进化),问有多 ...

  10. Ubuntu 16.04安装Insight实现汇编的调试

    由于Ubuntu从9.04开始就把Insight从APT源中删除,所以使用APT无法安装,而且<Assembly Language Step By Step, for Linux!>此书讲 ...