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
|
pub struct Solution {}
impl Solution {
pub fn max_satisfied(customers: Vec<i32>, grumpy: Vec<i32>, x: i32) -> i32 {
let x = x as usize;
let mut base: i32 = 0;
for (i, b) in grumpy.iter().enumerate() {
if b.eq(&0) {
base += customers.get(i).unwrap()
}
}
let mut max_increase = 0;
let mut increase = 0;
for (i, v) in customers.iter().enumerate() {
if i < x {
increase += v * grumpy.get(i).unwrap();
if i == x - 1 {
max_increase = increase;
}
} else {
increase = increase + v * grumpy.get(i).unwrap() - customers.get(i - x).unwrap() * grumpy.get(i - x).unwrap();
max_increase = max(max_increase, increase);
}
}
base + max_increase
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
assert_eq!(
16,
Solution::max_satisfied(
vec![1, 0, 1, 2, 1, 1, 7, 5],
vec![0, 1, 0, 1, 0, 1, 0, 1],
3,
)
);
}
}
|