Leetcode: 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".
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的更多相关文章
- [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- LeetCode Factorial Trailing Zeroes
原题链接在这里:https://leetcode.com/problems/factorial-trailing-zeroes/ 求factorial后结尾有多少个0,就是求有多少个2和5的配对. 但 ...
- 关于[LeetCode]Factorial Trailing Zeroes O(logn)解法的理解
题目描述: Given an integer n, return the number of trailing zeroes in n!. 题目大意: 给定一个整数n,返回n!(n的阶乘)结果中后缀0 ...
- [LeetCode] Ones and Zeroes 一和零
In the computer world, use restricted resource you have to generate maximum benefit is what we alway ...
- [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 ...
- 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 ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- [leetcode]Set Matrix Zeroes @ Python
原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...
- LeetCode Factorial Trailing Zeroes Python
Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. 题目意思: n求阶乘 ...
随机推荐
- iOS--NSBundle理解
NSBundle:官方文档解释:An NSBundle object represents a location in the file system that groups code and r ...
- asp.net c# 网上搜集面试题目大全(附答案)
1.String str=new String("a")和String str = "a"有什么区别? String str = "a"; ...
- 2016最新 wamp2.5+windows 10安装CoedSgniffer代码格式检查:
14:59 2016/1/112016最新 wamp2.5+windows 10安装CoedSgniffer代码格式检查:注意问题:1.手动安装2.5.0和pear安装方式都成功但是执行时无任何反映, ...
- JS:XML
一 IE中的XML //1.创建XMLDOM对象 //创建XMLDOM对象 var xmlDom = new ActiveXObject("MSXML2.DOMDocument.6.0&qu ...
- ZeroMQ接口函数之 :zmq_close - 关闭ZMQ socket
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_close zmq_close(3) ØMQ Manual - ØMQ/3.2.5 Name zmq_close ...
- JAVA面试题1
1.在main(String[] args)方法内是否可以调用一个非静态方法? 答案:不能[public static void main(String[] args){}] 2.同一个文件里是否可以 ...
- 11.static关键字
1.用static修饰的方法,直接用类调用 2.static修饰的方法只能调用static方法,不能调用非 static属性和方法 ①因为static属性和方法在类没有实例化的时候调用 ②因为普通属 ...
- HTML5表单
1.placeholder placeholder="e.g. King Kong" 只需在input元素中加入placeholder属性,其属性值就会默认显示为占位符文字,输入框 ...
- Visual Studio 2010编译时总是提示"调用目标发生了异常"的解决
现象: 无论建立的是Win32 Console的解决方案,还是MFC的解决方案,重新打开Visual Studio 2010之后,编译时总是提示“调用的目标发生了异常” 解决: 1. 关闭Visual ...
- js 获取指定日期
查询几天后的js代码,如果查询当天的日期 if($("input[name='startTime']").val()==""){ $("input[n ...