forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0474.cpp
51 lines (50 loc) · 1.45 KB
/
0474.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
int findMaxForm(vector<string>& strs, int m, int n)
{
vector<vector<int>> counter;
for (auto& per : strs)
{
int zeros = count(per.begin(), per.end(), '0');
int ones = count(per.begin(), per.end(), '1');
counter.push_back({ zeros, ones });
}
auto arr1 = counter;
sort(arr1.begin(), arr1.end(), [m, n](const vector<int>& s1, const vector<int>& s2) {
return min(m - s2[0], n - s2[1]) < min(m - s1[0], n - s1[1]);
});
auto arr2 = counter;
sort(arr2.begin(), arr2.end(), [](const vector<int>& s1, const vector<int>& s2) {
return min(s1[0], s1[1]) < min(s2[0], s2[1]);
});
return max(_findMaxForm(arr1, m, n), _findMaxForm(arr2, m, n));
}
private:
int _findMaxForm(vector<vector<int>>& arr, int m, int n)
{
int result = 0;
for (auto& per : arr)
{
if (m >= per[0] and n >= per[1])
{
++result;
m -= per[0];
n -= per[1];
}
}
return result;
}
};
int main()
{
vector<string> strs = {"10", "0001", "111001", "1", "0"};
int m = 5, n = 3;
cout << Solution().findMaxForm(strs, m, n);
return 0;
}