forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-number-of-ones.cpp
28 lines (26 loc) · 989 Bytes
/
maximum-number-of-ones.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
// Time: O(1)
// Space: O(1)
class Solution {
public:
int maximumNumberOfOnes(int width, int height, int sideLength, int maxOnes) {
if (width < height) {
swap(width, height);
}
const auto& R = height / sideLength, &r = height % sideLength;
const auto& C = width / sideLength, &c = width % sideLength;
assert(R <= C);
vector<pair<int, int>> area_counts = {{r * c, (R + 1) * (C + 1)},
{r * (sideLength - c), (R + 1) * C},
{(sideLength - r) * c, R * (C + 1)},
{(sideLength - r) * (sideLength - c), R * C}};
int result = 0;
for (const auto& [area, count] : area_counts) {
result += count * min(maxOnes, area);
maxOnes -= min(maxOnes, area);
if (!maxOnes) {
break;
}
}
return result;
}
};