Ones and Zeroes 问题及解法
2017-10-18 16:24
387 查看
问题描述:
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
On the other hand, there is an array with strings consisting of only
Now your task is to find the maximum number of strings that you can form with given m
Each
be used at most once.
Note:
The given numbers of
both not exceed
The size of given string array won't exceed
示例:
问题分析:
我们定义一种状态转移数组dp[i][j],表示i个0和j个1最多能组成几个字串,那么其转移过程就是dp[i][j] = max(dp[i][j], dp[i - zero][j - one] + 1);其中zero和one是当前字串中0
和1的个数。
过程详见代码:
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (auto s : strs)
{
int zero = 0,one = 0;
for (char ch : s)
{
if (ch == '0') zero++;
else one++;
}
for (int i = m; i >= zero; i--)
{
for (int j = n; j >= one; j--)
{
dp[i][j] = max(dp[i][j], dp[i - zero][j - one] + 1);
}
}
}
return dp[m]
;
}
};
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
0sand n
1srespectively.
On the other hand, there is an array with strings consisting of only
0sand
1s.
Now your task is to find the maximum number of strings that you can form with given m
0sand n
1s.
Each
0and
1can
be used at most once.
Note:
The given numbers of
0sand
1swill
both not exceed
100
The size of given string array won't exceed
600.
示例:
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 = 1 Output: 2 Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
问题分析:
我们定义一种状态转移数组dp[i][j],表示i个0和j个1最多能组成几个字串,那么其转移过程就是dp[i][j] = max(dp[i][j], dp[i - zero][j - one] + 1);其中zero和one是当前字串中0
和1的个数。
过程详见代码:
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
for (auto s : strs)
{
int zero = 0,one = 0;
for (char ch : s)
{
if (ch == '0') zero++;
else one++;
}
for (int i = m; i >= zero; i--)
{
for (int j = n; j >= one; j--)
{
dp[i][j] = max(dp[i][j], dp[i - zero][j - one] + 1);
}
}
}
return dp[m]
;
}
};
相关文章推荐
- LeetCode算法问题18 —— Ones and Zeroes
- 0-1背包问题:Ones and Zeroes
- 01背包问题(leetcode 416. Partition Equal Subset Sum & leetcode 474 ones and zeroes)
- 解决more than one device and emulator问题
- TSP问题的三种解法-hillclimbing,simulated_annealing and genetic algorithmg
- Encode and Decode TinyURL问题及解法
- poj 3517 And Then There Was One(约瑟夫问题)【模板】
- Best Time to Buy and Sell Stock II问题及解法
- Best Time to Buy and Sell Stock with Transaction Fee问题及解法
- LeetCode Ones and Zeroes
- Leetcode-474-Ones and Zeroes
- 3517 And Then There Was One 约瑟夫问题
- POJ 3517 And Then There Was One (递推,约瑟夫问题变形)
- Construct Binary Tree from Preorder and Inorder Traversal问题及解法
- Ones and Zeroes
- Construct Binary Tree from Inorder and Postorder Traversal问题及解法
- Serialize and Deserialize BST问题及解法
- Add One Row to Tree问题及解法
- UVALive 3882 - And Then There Was One【约瑟夫问题】
- Ones and Zeroes