【Leetcode】474. Ones and Zeroes
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:
- 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 = 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的更多相关文章
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】172. Factorial Trailing Zeroes
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your ...
- 【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 ...
- 【LeetCode】-- 73. Set Matrix Zeroes
问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...
- 【LeetCode】172. Factorial Trailing Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 递归 循环 日期 题目描述 Given an integer ...
- 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...
- 【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 ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- python之cookbook-day03
第一章:数据结构和算法 1.3 保留最后 N 个元素 问题: 在迭代操作或其他操作的时候,怎样只保留最后有限几个元素的历史记录? 解决方案: 保留有限历史记录正是 collections.deque ...
- CodeForces - 459C - Pashmak and Buses
先上题目+: C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input s ...
- codevs1197 Vigenère密码
题目描述 Description 16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码.Vigenère 密码的加密解密算法简单易用,且破译难度比 ...
- CentOS6.5 64位站点压力測试工具webbench
在Apache中有自带的ab命令能够測试服务的压力,而nginx没有自带的命令,必需要採用第三方软件来測试.今天就简介一下webbench对nginx的压力測试,压力測试是对系统管理员和运维人员必须的 ...
- 使用CDN
CDN的全称是Content Delivery Network.中文直译过来是:内容交付网络. 它的主要意思是,将某些内容进行交付的网络.对于站点开发而言,我们所讲的内容通常指的是内容文件(比如jav ...
- Spring技术内幕:Spring AOP的实现原理(五)
7.Advice通知的实现 AopProxy代理对象生成时,其拦截器也一并生成.以下我们来分析下Aop是怎样对目标对象进行增强的.在为AopProxy配置拦截器的实现中,有一个取得拦截器配置过程,这个 ...
- Jquery Mac OS Desktop项目想启动
想用Jquery做个MAC OS Desktop应用框架,欢迎大家提建议.
- LeetCode 720. Longest Word in Dictionary (字典里最长的单词)
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- 关于C语言指针的一些新认识(1)
Technorati 标签: 指针,数组,汇编,C语言 前言 指针是C语言的精华,但我对它一直有种敬而远之的感觉,因为一个不小心就可能让你的程序陷入莫名其妙的麻烦之中.所以,在处理字符串时,我总是能用 ...
- Spark 机器学习------逻辑回归
package Spark_MLlib import javassist.bytecode.SignatureAttribute.ArrayType import org.apache.spark.s ...