-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
37 lines (36 loc) · 1.51 KB
/
main.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
class Solution {
public:
int maximumNumberOfOnes(int width, int height, int sideLength, int maxOnes) {
vector<int> cnt(sideLength * sideLength, 0);
vector<vector<int>> mat(height, vector<int>(width));
for (int h = 0; h < height; h += sideLength) {
for (int w = 0; w < width; w += sideLength) {
for (int i = 0, k = 0; i < sideLength; ++i) {
int ii = h + i;
for (int j = 0; j < sideLength; ++j, ++k) {
int jj = w + j;
if (ii < 0 || height <= ii || jj < 0 || width <= jj) continue;
++cnt[k];
}
}
}
}
sort(cnt.begin(), cnt.end(), greater<>());
return accumulate(cnt.begin(), cnt.begin() + maxOnes, 0);
}
};
class Solution {
public:
int maximumNumberOfOnes(int width, int height, int sideLength, int maxOnes) {
vector<int> cnt(sideLength * sideLength, 0);
vector<vector<int>> mat(height, vector<int>(width));
for (int h = 0; h < height; h += sideLength)
for (int w = 0; w < width; w += sideLength)
for (int i = h, k = 0; i < h + sideLength; ++i)
for (int j = w; j < w + sideLength; ++j, ++k)
if (0 <= i && i < height && 0 <= j && j < width)
++cnt[k];
sort(cnt.begin(), cnt.end(), greater<>());
return accumulate(cnt.begin(), cnt.begin() + maxOnes, 0);
}
};