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 07b8b0c commit 19aa8dd
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

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

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

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

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

- 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.
39 changes: 39 additions & 0 deletions src/array/maximum_profit_of_operating_a_centennial_wheel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 经营摩天轮的最大利润
// https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel
// INLINE ../../images/array/maximum_profit_of_operating_a_centennial_wheel.jpeg

#include <headers.hpp>

class Solution {
public:
int minOperationsMaxProfit(vector<int> &customers, int boardingCost,
int runningCost) {
int profit = 0;
int maxProfit = 0;
int maxProfitRound = -1;
int waiting = 0;
int round = 0;
for (int customer : customers) {
waiting += customer;
int boarding = min(4, waiting);
waiting -= boarding;
profit += boarding * boardingCost - runningCost;
round++;
if (profit > maxProfit) {
maxProfit = profit;
maxProfitRound = round;
}
}
while (waiting > 0) {
int boarding = min(4, waiting);
waiting -= boarding;
profit += boarding * boardingCost - runningCost;
round++;
if (profit > maxProfit) {
maxProfit = profit;
maxProfitRound = round;
}
}
return maxProfitRound;
}
};
62 changes: 62 additions & 0 deletions test/array/maximum_profit_of_operating_a_centennial_wheel_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <array/maximum_profit_of_operating_a_centennial_wheel.cpp>

TEST(经营摩天轮的最大利润, minOperationsMaxProfit) {
Solution solution;
// 示例 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 。
vector<int> customers = {8, 3};
int boardingCost = 5;
int runningCost = 6;
EXPECT_EQ(
solution.minOperationsMaxProfit(customers, boardingCost, runningCost), 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 。
customers = {10, 9, 6};
boardingCost = 6;
runningCost = 4;
EXPECT_EQ(
solution.minOperationsMaxProfit(customers, boardingCost, runningCost), 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 。
customers = {3, 4, 0, 5, 1};
boardingCost = 1;
runningCost = 92;
EXPECT_EQ(
solution.minOperationsMaxProfit(customers, boardingCost, runningCost),
-1);
}
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 执行编译时间:2023-12-30 13:34:21
// 执行编译时间:2024-01-01 11:39:56
#include <gtest/gtest.h>
#include <lib.hpp>

Expand Down

0 comments on commit 19aa8dd

Please sign in to comment.