Week 10 - 474. Ones and Zeroes
474. Ones and Zeroes
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 and 1s will both not exceed 100
- 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".
my solution:
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<int> list;
bool flag = true;
for (vector<string>::iterator i = strs.begin(); i != strs.end(); i++) {
int count0 = 0, count1 = 0;
for (auto p : *i) { if (p == '0') count0++; else count1++; }
if ((m >= count0 && n >= count1)) {
flag = false;
vector<string> strs_cp = strs;
strs_cp.erase(strs_cp.begin() + (i - strs.begin()));
list.push_back(1 + findMaxForm(strs_cp, m - count0, n - ((*i).length() - count0)));
}
}
if (!flag) return *max_element(list.begin(), list.end()); else return 0;
}
};
在自己做这道题的过程中我使用的是递归的想法,能做出正确的答案,但是提交到leetcode显示Time limit exceeded。递归的做法虽然能做但是并没有利用到相同的子结构来降低复杂度。于是改出了一个非递归的做法。
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> memory(m + 1, vector<int>(n + 1, 0));
for (auto &s : strs) {
int count0 = 0, count1 = 0;
for (auto p : s) { if (p == '0') count0++; else count1++; }
for (int i = m; i >= count0; i--) {
for (int j = n; j >= count1; j--) {
memory[i][j] = max(memory[i][j], memory[i - count0][j - count1] + 1);
}
}
}
return memory[m][n];
}
};
用一个表来记录每组成一个字符串之后的状态。
Week 10 - 474. Ones and Zeroes的更多相关文章
- 【Leetcode】474. Ones and Zeroes
Today, Leet weekly contest was hold on time. However, i was late about 15 minutes for checking out o ...
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 474. Ones and Zeroes
In the computer world, use restricted resource you have to generate maximum benefit is what we alway ...
- 474 Ones and Zeroes 一和零
在计算机界中,我们总是追求用有限的资源获取最大的收益.现在,假设你分别支配着 m 个 0 和 n 个 1.另外,还有一个仅包含 0 和 1 字符串的数组.你的任务是使用给定的 m 个 0 和 n 个 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode算法总结
算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- SQLSERVER还原数据库失败:错误: 3154
在SQL Server 2008版本中还原从sql server 2005备份出来的数据库时,提示错误:“备份集中的数据库备份与现有的 '***' 数据库不同.RESTORE DATABASE 正在异 ...
- Ubuntu 14.04 下的MAC OS X 主题安装
Ubuntu 14.04 下的MAC OS X 主题安装 安装 MAC OS X 主题会帮助你的 Ubuntu 14.04 看起来更像MAC OS X.在这里我们介绍的Macbuntu安装包包含了GT ...
- SpringBoot项目优化和Jvm调优
https://www.cnblogs.com/jpfss/p/9753215.html 项目调优 作为一名工程师,项目调优这事,是必须得熟练掌握的事情. 在SpringBoot项目中,调优主要通过配 ...
- Verilog-2001标准在2001年就发布了
,不过翻了一些Verilog书籍,对Verilog-2001的新增特性很少有提及,即使提到了,也只是寥寥数语带过,其实在Verilog-2001中做了很多有用的改进,给编程带来很大的帮助,有必要详 ...
- CPU指令重排序与MESI缓存一致性
一.重排序场景 class ResortDemo { int a = 0; boolean flag = false; public void writer() { a = 1; //1 flag = ...
- maven 依赖显示红线 pom文件不显示红线的一种可能问题
pom文件引用的是CDH的jar包 而没有配置CDH的仓库 导致maven找不到资源 ,依赖显示红色波浪,并且在仓库内生成了一堆.lastupdate文件 解决: 1. 删除本地仓库内所有的.las ...
- CSP2019 题解
CSP2019 题解 D1T1 格雷码(code) 题目传送门 https://loj.ac/problem/3208 题解 按照题意模拟就可以了. 对于第 \(i\) 位,如果 \(k \geq 2 ...
- BAT面试必问题系列:深入详解JVM 内存区域及内存溢出分析
前言 在JVM的管控下,Java程序员不再需要管理内存的分配与释放,这和在C和C++的世界是完全不一样的.所以,在JVM的帮助下,Java程序员很少会关注内存泄露和内存溢出的问题.但是,一旦JVM发生 ...
- 10个你不得不知的WEB移动端开发的兼容问题
1.IOS下input设置type=button属性disabled设置true,会出现样式文字和背景异常问题,使用opacity=1来解决 2.一些情况下对非可点击元素如(label,span)监听 ...
- onmouseover和onmouseout鼠标移入移出切换图片的几种实现方法
鼠标移入移出图片切换很常见的,那我们就来说说他的写法 第一种方法,也是最简单的一种,在html:里就可实现 <img class="img" src="img/do ...