Skip to content

Commit

Permalink
add: 经营摩天轮的最大利润
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 1, 2024
1 parent 12fc211 commit 526a943
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [经营摩天轮的最大利润](src/array/maximum_profit_of_operating_a_centennial_wheel.rs) [数组, 模拟]

- LeetCode 1599. 经营摩天轮的最大利润 <https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel>

- [使用最小花费爬楼梯](src/array/min_cost_climbing_stairs.rs) [数组, 动态规划]

- LeetCode 746. 使用最小花费爬楼梯 <https://leetcode.cn/problems/min-cost-climbing-stairs>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions src/array/maximum_profit_of_operating_a_centennial_wheel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 经营摩天轮的最大利润
// https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel
// INLINE ../../images/array/maximum_profit_of_operating_a_centennial_wheel.jpeg

pub struct Solution;

impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32,
) -> i32 {
let mut profit = 0;
let mut waiting = 0;
let mut max_profit = 0;
let mut rotate = 0;
let mut max_rotate = 0; // 新增变量来记录达到最大利润时的旋转次数

for customer in customers {
waiting += customer;
let boarding = waiting.min(4);
waiting -= boarding;
profit += boarding * boarding_cost - running_cost;
rotate += 1;
if profit > max_profit {
max_profit = profit;
max_rotate = rotate; // 更新达到最大利润时的旋转次数
}
}
while waiting > 0 {
let boarding = waiting.min(4);
waiting -= boarding;
profit += boarding * boarding_cost - running_cost;
rotate += 1;
if profit > max_profit {
max_profit = profit;
max_rotate = rotate; // 更新达到最大利润时的旋转次数
}
}
if max_profit <= 0 {
-1
} else {
max_rotate // 返回达到最大利润时的旋转次数
}
}
}
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ pub mod number_of_times_binary_string_is_prefix_aligned;
pub mod house_robber;
pub mod stamping_the_grid;
pub mod min_cost_climbing_stairs;
pub mod maximum_profit_of_operating_a_centennial_wheel;
58 changes: 58 additions & 0 deletions tests/array/maximum_profit_of_operating_a_centennial_wheel_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use rust_practice::array::maximum_profit_of_operating_a_centennial_wheel::Solution;

#[test]
fn min_operations_max_profit() {
// 示例 1:
// 输入:customers = [8,3], boardingCost = 5, runningCost = 6
// 输出:3
// 解释:座舱上标注的数字是该座舱的当前游客数。
// 1. 8 位游客抵达,4 位登舱,4 位等待下一舱,摩天轮轮转。当前利润为 4 * $5 - 1 * $6 = $14 。
// 2. 3 位游客抵达,4 位在等待的游客登舱,其他 3 位等待,摩天轮轮转。当前利润为 8 * $5 - 2 * $6 = $28 。
// 3. 最后 3 位游客登舱,摩天轮轮转。当前利润为 11 * $5 - 3 * $6 = $37 。
// 轮转 3 次得到最大利润,最大利润为 $37 。
let customers = vec![8, 3];
let boarding_cost = 5;
let running_cost = 6;
assert_eq!(
Solution::min_operations_max_profit(customers, boarding_cost, running_cost),
3
);

// 示例 2:
// 输入:customers = [10,9,6], boardingCost = 6, runningCost = 4
// 输出:7
// 解释:
// 1. 10 位游客抵达,4 位登舱,6 位等待下一舱,摩天轮轮转。当前利润为 4 * $6 - 1 * $4 = $20 。
// 2. 9 位游客抵达,4 位登舱,11 位等待(2 位是先前就在等待的,9 位新加入等待的),摩天轮轮转。当前利润为 8 * $6 - 2 * $4 = $40 。
// 3. 最后 6 位游客抵达,4 位登舱,13 位等待,摩天轮轮转。当前利润为 12 * $6 - 3 * $4 = $60 。
// 4. 4 位登舱,9 位等待,摩天轮轮转。当前利润为 * $6 - 4 * $4 = $80 。
// 5. 4 位登舱,5 位等待,摩天轮轮转。当前利润为 20 * $6 - 5 * $4 = $100 。
// 6. 4 位登舱,1 位等待,摩天轮轮转。当前利润为 24 * $6 - 6 * $4 = $120 。
// 7. 1 位登舱,摩天轮轮转。当前利润为 25 * $6 - 7 * $4 = $122 。
// 轮转 7 次得到最大利润,最大利润为$122 。
let customers = vec![10, 9, 6];
let boarding_cost = 6;
let running_cost = 4;
assert_eq!(
Solution::min_operations_max_profit(customers, boarding_cost, running_cost),
7
);

// 示例 3:
// 输入:customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
// 输出:-1
// 解释:
// 1. 3 位游客抵达,3 位登舱,0 位等待,摩天轮轮转。当前利润为 3 * $1 - 1 * $92 = -$89 。
// 2. 4 位游客抵达,4 位登舱,0 位等待,摩天轮轮转。当前利润为 7 * $1 - 2 * $92 = -$177 。
// 3. 0 位游客抵达,0 位登舱,0 位等待,摩天轮轮转。当前利润为 7 * $1 - 3 * $92 = -$269 。
// 4. 5 位游客抵达,4 位登舱,1 位等待,摩天轮轮转。当前利润为 11 * $1 - 4 * $92 = -$357 。
// 5. 1 位游客抵达,2 位登舱,0 位等待,摩天轮轮转。当前利润为 13 * $1 - 5 * $92 = -$447 。
// 利润永不为正,所以返回 -1 。
let customers = vec![3, 4, 0, 5, 1];
let boarding_cost = 1;
let running_cost = 92;
assert_eq!(
Solution::min_operations_max_profit(customers, boarding_cost, running_cost),
-1
);
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ pub mod number_of_times_binary_string_is_prefix_aligned_test;
pub mod house_robber_test;
pub mod stamping_the_grid_test;
pub mod min_cost_climbing_stairs_test;
pub mod maximum_profit_of_operating_a_centennial_wheel_test;

0 comments on commit 526a943

Please sign in to comment.