-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_max_division.rs
61 lines (51 loc) · 1.35 KB
/
min_max_division.rs
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
52
53
54
55
56
57
58
59
60
61
struct MinMaxDivision;
impl MinMaxDivision {
fn check(a: &Vec<i32>, k_max_block_count: i32, mid_sum_candidate: i32) -> bool {
let mut blocks = 1;
let mut sum = 0;
for e in a {
sum += *e;
if sum > mid_sum_candidate {
blocks += 1;
sum = *e;
}
if blocks > k_max_block_count {
return false;
}
}
true
}
fn solution(k: i32, _m: i32, a: &mut Vec<i32>) -> i32 {
let n = a.len();
let mut min_sum = *a.iter().max().unwrap();
let mut max_sum = a.iter().sum();
if n == 1 {
return min_sum;
}
if k == 1 {
return max_sum;
}
let mut result = min_sum;
while min_sum <= max_sum {
let mid_sum_candidate = (min_sum + max_sum) / 2;
if MinMaxDivision::check(a, k, mid_sum_candidate) {
result = mid_sum_candidate;
max_sum = mid_sum_candidate - 1;
} else {
min_sum = mid_sum_candidate + 1;
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_min_max_division() {
assert_eq!(
MinMaxDivision::solution(3, 5, &mut vec![2, 1, 5, 1, 2, 2, 2]),
6
);
}
}