[leetcode-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:

  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".

思路:

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

这道题是一道典型的应用DP来解的题,如果我们看到这种求总数,而不是列出所有情况的题,十有八九都是用DP来解,重中之重就是在于找出递推式。如果你第一反应没有想到用DP来做,想得是用贪心算法来做,比如先给字符串数组排个序,让长度小的字符串在前面,然后遍历每个字符串,遇到0或者1就将对应的m和n的值减小,这种方法在有的时候是不对的,比如对于{"11", "01", "10"},m=2,n=2这个例子,我们将遍历完“11”的时候,把1用完了,那么对于后面两个字符串就没法处理了,而其实正确的答案是应该组成后面两个字符串才对。所以我们需要建立一个二位的DP数组,其中dp[i][j]表示有i个0和j个1时能组成的最多字符串的个数,而对于当前遍历到的字符串,我们统计出其中0和1的个数为zeros和ones,然后dp[i - zeros][j - ones]表示当前的i和j减去zeros和ones之前能拼成字符串的个数,那么加上当前的zeros和ones就是当前dp[i][j]可以达到的个数,我们跟其原有数值对比取较大值即可,所以递推式如下:

dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1);

有了递推式,我们就可以很容易的写出代码如下:

int findMaxForm(vector<string>& strs, int m, int n)
     {
         vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
         int ones , zeros ;

         for (string str : strs)
         {
             ones = 0, zeros = 0;
             for (char ch : str)
             {
                 if (ch == ‘0‘)zeros++;
                 else if (ch == ‘1‘)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];
     }
时间: 2024-10-16 08:28:53

[leetcode-474-Ones and Zeroes]的相关文章

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某

【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. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

Java [Leetcode 172]Factorial Trailing Zeroes

题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解题思路: 对于阶乘而言,也就是1*2*3*...*n[n/k]代表1~n中能被k整除的个数那么很显然[n/2] > [n/5] (左边是逢2增1,右边是逢5增1)[n/2^2] > [n/5^2](左边是逢4增1,右边是逢25增1)

LeetCode Day4——Factorial Trailing Zeroes

1 /* 2 * Problem 172: Factorial Trailing Zeroes 3 * Given an integer n, return the number of trailing zeroes in n!. 4 * Note: Your solution should be in logarithmic time complexity. 5 */ 6 7 /* 8 * Solution 1 9 * 对于每一个数字,累计计算因子10.5.2数字出现的个数,结果等于10出现的

Java for LeetCode 172 Factorial Trailing Zeroes

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 解题思路: 计算n能达到的5的最大次幂,算出在这种情况下能提供的5的个数,然后减去之后递归即可,JAVA实现如下: static public int trailingZeroes(int n) { if(n<25) return n/5; lon

【LeetCode】Factorial Trailing Zeroes

n久不做题了 ,之前因为考研,然后又是假期,一直懒得做,今天开始吧 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 开始没有看到是阶乘,之后又研究复杂度的问题 代码如下: class Solution { public:     int trailingZeroes(int n) {      

LeetCode 172 Factorial Trailing Zeroes(阶乘后的零)(*)

翻译 给定一个整型n,返回n!后面的零的个数. 注意:你的解决方案应该在log时间复杂度内. 原文 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 起初我看题目的时候没太注意,还以为就是求n这个数后面的零而已,虽然心想不会这么简单吧--就写了一份代码提交了,结果WA提示我5的话应该返回1,

leetcode——172 Factorial Trailing Zeroes(N!尾巴上有多少个0,算法复杂度为lg)

Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity.(您的解决方案应该在对数时间复杂度.) Hide Tags: Math 题目要求:给定N,求N!的末尾有多少0.要求算法复杂度为lg 解题思路: 思路一: 想的比较简单,先实用for循环进行阶乘运算,然后mod10计算0的个数,但是在OJ检查时,超时

[C++]LeetCode: 88 Factorial Trailing Zeroes (阶乘后导零)

题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 思路: 我们要计算 N! 中有多少个后导0. 我们来找一下规律,考虑n!的质数因子.后缀0,只有可能是质因子2 * 质因子5得到.如果我们可以计算得到min{num(2), num(5)},就可以知道后导0的个数. 举例子: n = 5!