add: 220523 [cpp]
This commit is contained in:
parent
1032df9f65
commit
c4334f67cc
|
|
@ -0,0 +1,41 @@
|
|||
#include <vector>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
/**
|
||||
* 474. Ones and Zeroes
|
||||
* You are given an array of binary strings strs and two integers m and n.
|
||||
* Return the size of the largest subset of strs such that there are at most m 0's and n 1's in the subset.
|
||||
* A set x is a subset of a set y if all elements of x are also elements of y.
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
static int findMaxForm(const std::vector<std::string>& strs, int m, int n) {
|
||||
std::vector<uint16_t> s;
|
||||
s.reserve(strs.size());
|
||||
for (const std::string& str : strs) {
|
||||
s.push_back((std::count(str.begin(), str.end(), '0') << 8) | std::count(str.begin(), str.end(), '1'));
|
||||
}
|
||||
|
||||
int ans[101][101]{};
|
||||
|
||||
for (int c : s) {
|
||||
int cm = c >> 8, cn = c & 0xFF;
|
||||
for (int im = m; im >= cm; --im) {
|
||||
for (int in = n; in >= cn; --in) {
|
||||
ans[im][in] = std::max(ans[im][in], 1 + ans[im - cm][in - cn]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ans[m][n];
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << Solution::findMaxForm({"10","0001","111001","1","0"}, 5, 3);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2205)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2205 220523-CN.cpp)
|
||||
ADD_EXECUTABLE(2205 220523.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue