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的更多相关文章

  1. 【Leetcode】474. Ones and Zeroes

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

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

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

  3. 474. Ones and Zeroes

    In the computer world, use restricted resource you have to generate maximum benefit is what we alway ...

  4. 474 Ones and Zeroes 一和零

    在计算机界中,我们总是追求用有限的资源获取最大的收益.现在,假设你分别支配着 m 个 0 和 n 个 1.另外,还有一个仅包含 0 和 1 字符串的数组.你的任务是使用给定的 m 个 0 和 n 个 ...

  5. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  6. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  7. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  8. leetcode算法总结

    算法思想 二分查找 贪心思想 双指针 排序 快速选择 堆排序 桶排序 搜索 BFS DFS Backtracking 分治 动态规划 分割整数 矩阵路径 斐波那契数列 最长递增子序列 最长公共子系列 ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. DRF之Jwt 实现自定义和DRF小组件及django-filter插件的使用

    一.DRF之Jwt 实现自定义 二.DRF(过滤,排序,分页)组件 三.Django-filter插件的使用和自定义 """ 1.drf-jwt手动签发与校验 :只是做t ...

  2. CRMEasy知识库访问权限

    知识库设置BCCBINFO文件夹为共享文件夹,给予普通域用户和管理员域用户完全控制权限,但是当以管理员域用户身份对文件进行操作后,以普通域用户登录不会有效果,甚至出现“访问权限”的错误,这是由于文件夹 ...

  3. [易学易懂系列|golang语言|零基础|快速入门|(一)]

    golang编程语言,是google推出的一门语言. 主要应用在系统编程和高性能服务器编程,有广大的市场前景,目前整个生态也越来越强大,未来可能在企业应用和人工智能等领域占有越来越重要的地位. 本文章 ...

  4. 九、ARM 汇编与 C 的混合编程

    9.1 ARM 汇编与 C 的混合编程 9.1.1 内嵌汇编 __asm __asm("指令")例如关闭/打开总中断开关 CPSR __asm //使用 C 中变量名代替寄存器 { ...

  5. 组件通信 eventtBus

    平级组件的通信 一个全局发布订阅模式,它是挂载到全局的 <!DOCTYPE html> <html lang="en"> <head> < ...

  6. 【LuoguP3747】[六省联考2017] 相逢是问候

    题目链接 题意 给定一个长度为 n 的序列 a , 给定一个正整数 c 每次修改操作是把一段区间内的数 \(x_i\) 修改为 \(c^{x_i}\) 询问区间和模 p 的结果 Sol 修改是把一个数 ...

  7. hadoop2.7伪分布式搭建

    0.配置主机名 hostnamectl set-hostname spark1 1.上传hadoop-2.7.1.tar.gz文件并解压 tar -xvf hadoop-2.7.1.tar.gz 2. ...

  8. 【leetcode】1169. Invalid Transactions

    题目如下: A transaction is possibly invalid if: the amount exceeds $1000, or; if it occurs within (and i ...

  9. jquery +点击按钮,切换div内容,按钮加高亮

    html: <div class="dw4"> <span class="dw">单位(次)</span> <div ...

  10. node创建项目,要ejs模板引擎,不要jade怎么办?

    创建项目时:express ejs blog,生成的是.jade文件怎么办?我想要的是ejs的呀 上述语句不仅名字不对,生成文件的格式也不是我想要的. 不妨试试:express -e blog 这下就 ...