-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01052. Grumpy Bookstore Owner.rs
49 lines (43 loc) · 1.6 KB
/
01052. Grumpy Bookstore Owner.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
use std::cmp;
impl Solution {
pub fn max_satisfied(mut customers: Vec<i32>, mut grumpy: Vec<i32>, x: i32) -> i32 {
let size = customers.len();
let mut biggest_grumpy_of_x = 0;
let mut total_grumpy = 0;
let mut total_satisfied = 0;
let mut i = 0;
loop {
if (i == size) {
break;
}
// Calculate total grumpy customers
if (grumpy[i] == 1) {
total_grumpy += customers[i];
} else {
// Calculate total satisfied customers and update customers vector to only show grumpy customers
total_satisfied += customers[i];
customers[i] = 0;
}
// Calculate biggest total of x grumpy customers till now
if (i as i32 > (x - 2)) {
let mut j: i32 = (i as i32 - (x - 1));
let mut total_of_x = 0;
loop {
if (j as usize == (i + 1)) {
break;
}
total_of_x += customers[j as usize];
j += 1;
}
biggest_grumpy_of_x = cmp::max(biggest_grumpy_of_x, total_of_x);
}
i += 1;
}
/*println!("{:?}", customers);
println!("{}", biggest_grumpy_of_x);
println!("{}", total_grumpy);
println!("{}", total_satisfied);*/
// Return total satisfied + biggest grumpy of x items
return total_satisfied + biggest_grumpy_of_x;
}
}