题目
你正在经营一座摩天轮,该摩天轮共有 4 个座舱 ,每个座舱 最多可以容纳 4 位游客 。你可以 逆时针 轮转座舱,但每次轮转都需要支付一定的运行成本 runningCost 。摩天轮每次轮转都恰好转动 1 / 4 周。
给你一个长度为 n 的数组 customers , customers[i] 是在第 i 次轮转(下标从 0 开始)之前到达的新游客的数量。这也意味着你必须在新游客到来前轮转 i 次。每位游客在登上离地面最近的座舱前都会支付登舱成本 boardingCost ,一旦该座舱再次抵达地面,他们就会离开座舱结束游玩。
你可以随时停下摩天轮,即便是 在服务所有游客之前 。如果你决定停止运营摩天轮,为了保证所有游客安全着陆,将免费进行所有后续轮转 。注意,如果有超过 4 位游客在等摩天轮,那么只有 4 位游客可以登上摩天轮,其余的需要等待 下一次轮转 。
返回最大化利润所需执行的 最小轮转次数 。 如果不存在利润为正的方案,则返回 -1 。
示例
示例1:
输入:customers = [8,3], boardingCost = 5, runningCost = 6
输出:3
示例2:
输入:customers = [10,9,6], boardingCost = 6, runningCost = 4
输出:7
示例3:
输入:customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
输出:-1
示例4:
输入:customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8
输出:9
解释:
- 10 位游客抵达,4 位登舱,6 位等待,摩天轮轮转。当前利润为 4 * $3 - 1 * $8 = $4 。
- 10 位游客抵达,4 位登舱,12 位等待,摩天轮轮转。当前利润为 8 * $3 - 2 * $8 = $8 。
- 6 位游客抵达,4 位登舱,14 位等待,摩天轮轮转。当前利润为 12 * $3 - 3 * $8 = $12 。
- 4 位游客抵达,4 位登舱,14 位等待,摩天轮轮转。当前利润为 16 * $3 - 4 * $8 = $16 。
- 7 位游客抵达,4 位登舱,17 位等待,摩天轮轮转。当前利润为 20 * $3 - 5 * $8 = $20 。
- 4 位登舱,13 位等待,摩天轮轮转。当前利润为 24 * $3 - 6 * $8 = $24 。
- 4 位登舱,9 位等待,摩天轮轮转。当前利润为 28 * $3 - 7 * $8 = $28 。
- 4 位登舱,5 位等待,摩天轮轮转。当前利润为 32 * $3 - 8 * $8 = $32 。
- 4 位登舱,1 位等待,摩天轮轮转。当前利润为 36 * $3 - 9 * $8 = $36 。
- 1 位登舱,0 位等待,摩天轮轮转。当前利润为 37 * $3 - 10 * $8 = $31 。
经过以上分析得出,轮转 9 次得到最大利润,最大利润为 $36 。
说明
- n == customers.length
- 1 <= n <= 105
- 0 <= customers[i] <= 50
- 1 <= boardingCost, runningCost <= 100
Python实现
class Solution(object):
def minOperationsMaxProfit(self, customers, boardingCost, runningCost):
"""
:type customers: List[int]
:type boardingCost: int
:type runningCost: int
:rtype: int
"""
wait = 0
x1 = 0
money = 0
num = 0
time = 0
for i in range(len(customers)):
time += 1
wait += customers[i]
if wait > 4 :
x1 += 4
wait -= 4
else:
x1 += wait
wait = 0
result = x1*boardingCost - time*runningCost
# print(x1,time,result,wait)
if result > money :
money = result
num = time
while wait > 0:
time += 1
if wait > 4 :
x1 += 4
wait -= 4
else:
x1 += wait
wait = 0
result = x1*boardingCost - time*runningCost
# print(x1,time,result,wait)
if result > money :
money = result
num = time
if num == 0:return -1
else:return num