diff --git a/README.md b/README.md index 44ea7e7..006ded9 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文 ### 数组/队列/集合/映射 +- [经营摩天轮的最大利润](src/array/maximum_profit_of_operating_a_centennial_wheel.cpp) [数组, 模拟] + + - LeetCode 1599. 经营摩天轮的最大利润 + - [使用最小花费爬楼梯](src/array/min_cost_climbing_stairs.cpp) [数组, 动态规划] - LeetCode 746. 使用最小花费爬楼梯 diff --git a/images/array/maximum_profit_of_operating_a_centennial_wheel.jpeg b/images/array/maximum_profit_of_operating_a_centennial_wheel.jpeg new file mode 100644 index 0000000..5bb6544 Binary files /dev/null and b/images/array/maximum_profit_of_operating_a_centennial_wheel.jpeg differ diff --git a/src/array/maximum_profit_of_operating_a_centennial_wheel.cpp b/src/array/maximum_profit_of_operating_a_centennial_wheel.cpp new file mode 100644 index 0000000..e7649fd --- /dev/null +++ b/src/array/maximum_profit_of_operating_a_centennial_wheel.cpp @@ -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 + +class Solution { +public: + int minOperationsMaxProfit(vector &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; + } +}; \ No newline at end of file diff --git a/test/array/maximum_profit_of_operating_a_centennial_wheel_test.cpp b/test/array/maximum_profit_of_operating_a_centennial_wheel_test.cpp new file mode 100644 index 0000000..b3ecb09 --- /dev/null +++ b/test/array/maximum_profit_of_operating_a_centennial_wheel_test.cpp @@ -0,0 +1,62 @@ +#include + +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 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); +} diff --git a/test/lib/lib_test.cpp b/test/lib/lib_test.cpp index db9f0ee..a531370 100644 --- a/test/lib/lib_test.cpp +++ b/test/lib/lib_test.cpp @@ -1,4 +1,4 @@ -// 执行编译时间:2023-12-30 13:34:21 +// 执行编译时间:2024-01-01 11:39:56 #include #include