-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
src/array/maximum_profit_of_operating_a_centennial_wheel.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
test/array/maximum_profit_of_operating_a_centennial_wheel_test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|