问题描述
输入一组字符串Array,其中的每个字符串中只可能出现’0’和’1’两种字符;给定两个整数m和n。现在从这组字符串中选取一些字符串,求在保证所选字符串中’0’的个数之和不超过m、’1’的个数之和不超过n的前提下,能够选取到的字符串的最大个数。例如:
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
Output: 4
Output: 2
Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
输入一组字符串Array,其中的每个字符串中只可能出现’0’和’1’两种字符;给定两个整数m和n。现在从这组字符串中选取一些字符串,求在保证所选字符串中’0’的个数之和不超过m、’1’的个数之和不超过n的前提下,能够选取到的字符串的最大个数。例如:
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”.
Input: Array = {"10", "0", "1"}, m = 1, n = 1Output: 2
Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
---------------------------
一、Brute Force
找出Array中包含字符串个数分别为1, 2, 3…, Array.length – 1, Array.length的所有可能组合;然后判断各个组合是否满足m和n分别关于’0’和’1’的约束;满足条件的组合中包含最多字符串的组合的长度即为所求。
二、回溯算法