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

This is a 0/1 backpacking problem

The problem can be interpreted as: What's the max number of str can we pick from strs with limitation of m "0"s and n "1"s. Thus we can define dp[i][j] stands for max number of str can we pick from strs with limitation of i "0"s and j "1"s. For each str, assume it has a "0"s and b "1"s, we update the dp array iteratively and set dp[i][j] = Math.max(dp[i][j], dp[i - a][j - b] + 1). So at the end, dp[m][n] is the answer.

the optimal code refer to https://discuss.leetcode.com/topic/71417/java-iterative-dp-solution-o-mn-space/5

Time Complexity: O(kl + kmn), where k is the length of input string array and l is the average length of a string within the array.

Space Complexity: O(mn)

(My thinking: )This solution applies the 'dimension-reduction strategy', otherwise the space is O(mnk). The strategy is to reverse the scaning direction from end of the array to start.

 public class Solution {
public int findMaxForm(String[] strs, int m, int n) {
int[][] dp = new int[m+1][n+1];
for (String str : strs) {
int[] cost = count(str);
for (int i=m; i>=cost[0]; i--) {
for (int j=n; j>=cost[1]; j--) {
dp[i][j] = Math.max(dp[i-cost[0]][j-cost[1]]+1, dp[i][j]);
}
}
}
return dp[m][n];
} public int[] count(String str) {
int[] count = new int[2];
for (int i=0; i<str.length(); i++) {
count[(int)(str.charAt(i)-'0')]++;
}
return count;
}
}

Leetcode: Ones and Zeroes的更多相关文章

  1. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  2. LeetCode Factorial Trailing Zeroes

    原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...

  3. 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解

    题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...

  4. [LeetCode] Ones and Zeroes 一和零

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

  5. [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 ...

  6. LeetCode 283. Move Zeroes (移动零)

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  7. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  8. [leetcode]Set Matrix Zeroes @ Python

    原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...

  9. LeetCode Factorial Trailing Zeroes Python

    Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...

随机推荐

  1. UI--UIPickerView和UIDatePicker的总结

    回到顶部 UIPickerView的主要方法和城市选择器的修正 UIPickerView只有两个数据源方法.要想完整地显示出PickerView,需要结合使用代理方法 数据源方法: // 一共有多少组 ...

  2. php文字、图片水印功能函数封装

    一直在做有关php文字图片上传方面的工作,所以把此功能函数整理了一次,现在分享给大家. <?php class image { var $g_img; var $g_w; var $g_h; v ...

  3. 常见bug及解决方案

    1.外边距叠加 一.发生在一个div内 <!DOCTYPE> <html> <head> <meta http-equiv=Content-Type cont ...

  4. MySQL数据备份之mysqldump使用

    mysqldump常用于MySQL数据库逻辑备份. 1.各种用法说明 A. 最简单的用法: mysqldump -uroot -pPassword [database name] > [dump ...

  5. thinkPHP访问不同表前缀

    $Model=new Model(); $goods=$Model->Table('sdb_goods'); $param['brief']=array('like','%'.$p_title. ...

  6. Django入门实践(3)

    Django简单应用 前面简单示例说明了views和Template的工作过程,但是Django最核心的是App,涉及到App则会和Model(数据库)打交道.下面举的例子是创建一个简单应用wiki ...

  7. 使用File类列出指定位置的文件信息,包含该路径子目录下的文件信息

    public class Test{ public static void main(String [] args) { File f=new File("d:"); File [ ...

  8. Jvascript简介

    一.Javascript就是我们所说的脚本语言.它不同于C++/java等语言,它更加灵活! 正因为其灵活性,没有那么多的规章制度,也是我们容易学的地方,但很多时候也是 令人无奈的地方! 二.我们要知 ...

  9. xdebug所有相关方法函数详解(中文翻译版)

    此次翻译部分借助google翻译,如有错误,请联系qq:903464207反馈问题,或者留言反馈 翻译时间:2016年4月18日09:41:34 xdebug.remote_enable = onxd ...

  10. JavaScript slice() 方法

    JavaScript slice() 方法  JavaScript Array 对象 实例 在数组中读取元素: var fruits = ["Banana", "Oran ...