-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01176. Diet Plan Performance.rs
35 lines (33 loc) · 1.13 KB
/
01176. Diet Plan Performance.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
impl Solution {
pub fn diet_plan_performance(calories: Vec<i32>, k: i32, lower: i32, upper: i32) -> i32 {
let mut i = 0;
let mut j = 0;
let mut points = 0;
let total_days = calories.len();
let mut calories_to_count = 0;
loop {
if ((i + k) > (total_days) as i32) {
break;
}
// Add up calories from day j to (j + k). Then check if it is more than upper or less than lower and calculate points accordingly. Keep moving j with i
j = i;
calories_to_count = 0;
loop {
if (j == (i + k)) {
break;
}
calories_to_count += calories[(j) as usize];
j += 1;
}
if (calories_to_count < lower) {
println!("l {}, {}", calories_to_count, lower);
points -= 1;
} else if (calories_to_count > upper) {
println!("u {}, {}", calories_to_count, upper);
points += 1;
}
i += 1;
}
return points;
}
}