-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMinMaxDivision.cpp
More file actions
50 lines (43 loc) · 1.37 KB
/
MinMaxDivision.cpp
File metadata and controls
50 lines (43 loc) · 1.37 KB
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
#include <numeric>
#include <algorithm>
// result: https://app.codility.com/demo/results/training4NW8N5-TKB/
bool check(vector<int>& A, int K_MaxBlockCount, int MidSumCandidate) {
int blocks = 1;
int sum = 0;
for (auto i : A) {
sum += i;
if (sum > MidSumCandidate) {
blocks++;
sum = i;
}
if (blocks > K_MaxBlockCount)
return false;
}
return true;
}
// NOTE: do not use M, it is incorrect in some test cases.
// you need to calculate it yourself
int solution(int K_MaxBlockCount, int /*M*/, vector<int> &A) {
// using binary search to find optimum block counts
// minimum large sum
int min_sum = *std::max_element(A.begin(), A.end());
// maximum large sum
int max_sum = std::accumulate(A.begin(), A.end(), 0);
if (A.size() == 1)
return min_sum;
if (K_MaxBlockCount == 1)
return max_sum;
int result = min_sum;
while (min_sum <= max_sum) {
int mid_sum_candidate = (min_sum + max_sum) / 2;
// check if we can make maximum possible blocks (<K) that have maximum large sum up to mid_sum_candidate
if (check(A, K_MaxBlockCount, mid_sum_candidate)) {
result = mid_sum_candidate;
max_sum = mid_sum_candidate - 1;
}
else {
min_sum = mid_sum_candidate + 1;
}
}
return result;
}