Skip to main content

rustgym/leetcode/
_1599_maximum_profit_of_operating_a_centennial_wheel.rs

1struct Solution;
2
3impl Solution {
4    fn min_operations_max_profit(
5        customers: Vec<i32>,
6        boarding_cost: i32,
7        running_cost: i32,
8    ) -> i32 {
9        let mut wait = 0;
10        let mut profit = 0;
11        let mut max_profit = 0;
12        let mut res = -1;
13        let mut run = 0;
14        while run < customers.len() || wait > 0 {
15            if run < customers.len() {
16                wait += customers[run];
17            }
18            profit += wait.min(4) * boarding_cost - running_cost;
19            wait -= wait.min(4);
20            if max_profit < profit {
21                max_profit = profit;
22                res = run as i32 + 1;
23            }
24            run += 1;
25        }
26        res
27    }
28}
29
30#[test]
31fn test() {
32    let customers = vec![8, 3];
33    let boarding_cost = 5;
34    let running_cost = 6;
35    let res = 3;
36    assert_eq!(
37        Solution::min_operations_max_profit(customers, boarding_cost, running_cost),
38        res
39    );
40}