在计算机界中,我们总是追求用有限的资源获取最大的收益。
现在,假设你分别支配着 m 个 0 和 n 个 1。另外,还有一个仅包含 0 和 1 字符串的数组。
你的任务是使用给定的 m 个 0 和 n 个 1 ,找到能拼出存在于数组中的字符串的最大数量。每个 0 和 1 至多被使用一次。
注意:
    给定 0 和 1 的数量都不会超过 100。
    给定字符串数组的长度不会超过 600。
示例 1:
输入: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
输出: 4
解释: 总共 4 个字符串可以通过 5 个 0 和 3 个 1 拼出,即 "10","0001","1","0" 。

示例 2:
输入: Array = {"10", "0", "1"}, m = 1, n = 1
输出: 2
解释: 你可以拼出 "10",但之后就没有剩余数字了。更好的选择是拼出 "0" 和 "1" 。
详见:https://leetcode.com/problems/ones-and-zeroes/description/

C++:

class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n)
{
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (string str : strs)
{
int zeros = 0, ones = 0;
for (char c : str)
{
(c == '0') ? ++zeros : ++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] + 1);
}
}
}
return dp[m][n];
}
};

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

474 Ones and Zeroes 一和零的更多相关文章

  1. [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

    1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...

  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】474. Ones and Zeroes 解题报告(Python)

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

  5. [LeetCode] Preimage Size of Factorial Zeroes Function 阶乘零的原像个数函数

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  6. 8.Move Zeroes(移动零)

    Level:   Easy 题目描述: Given an array nums, write a function to move all 0's to the end of it while mai ...

  7. 073 Set Matrix Zeroes 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...

  8. [LeetCode] Ones and Zeroes 一和零

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

  9. [LeetCode] 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. click ...

随机推荐

  1. monggodb 复制集 集群 搭建

    https://docs.mongodb.com/manual/tutorial/enable-authentication/ Overview Enabling access control on ...

  2. 探索C++的底层机制

    探索C++的底层机制 在看这篇文章之前,请你先要明白一点:那就是c++为我们所提供的各种存取控制仅仅是在编译阶段给我们的限制,也就是说是编译器确保了你在完成任务之前的正确行为,如果你的行为不正确,那么 ...

  3. 时光轴一之listView实现时光轴效果

    尼玛.非要搞什么时光轴,一想简单的不就是个listView吗,然后一步一步来就好了,哈哈别看那么好看事实上不要想多了. 时光轴timeline最大的作用就是把过去的事物系统化.完整化.精确化.时间轴可 ...

  4. rails elasticsearch searchkick用法

    1.安装elasticsearch 之前要先安装java8: 参考https://www.elastic.co/guide/en/elasticsearch/reference/current/zip ...

  5. MAC Safari上网弹窗弹广告的最新有效解决方法

    7.3更新: 之前更改DNS好了一段时间,最近在打开其它网页时还是会弹广告: 最终解决方法: 安装MALWAREBYTES 3清理一下: 网址:Free Cyber Security & An ...

  6. Uva 3902 Network

    题目大意: 在非叶子节点上安装最少的服务器使得,每个叶子节点到服务器的距离不超过k. 贪心+图上的dfs. 先从深度最大的叶子节点开始找.找到父节点后再用这个父节点进行扩充. /* ********* ...

  7. codeforces B. Balls Game 解题报告

    题目链接:http://codeforces.com/problemset/problem/430/B 题目意思:给出用不多于k种颜色对n个球的染色情况,以及手中的唯一一个球的颜色.初始时,连续的相同 ...

  8. HDU - 1874 畅通工程续(最短路径)

    d.已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. s.最短路径 c.Dijkstra单源最短路 /* Dijkstra单源最短路 权值必须是非负 单源最短路径,Dijkstra算法 ...

  9. 奶牛排序——RMQ

    [问题描述]奶牛在熊大妈的带领下排成了一条直队.显然,不同的奶牛身高不一定相同……现在,奶牛们想知道,如果找出一些连续的奶牛,要求最左边的奶牛 A 是最矮的,最右边的 B 是最高的,且 B 高于 A ...

  10. darwin Stream Server源码分析

    摘要 ​所谓的流式媒体简单的讲就是指人们通过网络实时的收看多媒体信息:如音频流.视频流等.与流式媒体对应的传统工作方式是下载+播放模式,即用户首先下载多媒体文件,然后再在本地播放,这种方法的一个主要缺 ...