当前位置: 代码迷 >> 综合 >> Leetcode 1599. Maximum Profit of Operating a Centennial Wheel (python)
  详细解决方案

Leetcode 1599. Maximum Profit of Operating a Centennial Wheel (python)

热度:91   发布时间:2023-11-26 06:44:15.0

Leetcode 1599. Maximum Profit of Operating a Centennial Wheel

  • 题目
  • 解法:

题目

题目太长,直接放链接吧 https://leetcode.com/problems/maximum-profit-of-operating-a-centennial-wheel/

解法:

仔细地读懂题目不难做,就是greedy的思想

一开始写了一个非常丑的版本:

class Solution:def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:profit = 0waiting = 0rotation = 0max_profit = 0ans = Nonefor customer in customers:customer += waitingrotation += 1if customer>=4:profit += 4*boardingCost - runningCostwaiting = customer-4else:profit = customer*boardingCost - runningCostwaiting = 0if max_profit<profit:max_pprofit = profitans = rotationif waiting>0:if waiting>4:while waiting>4:profit += 4*boardingCost - runningCostwaiting = waiting-4rotation += 1#print(profit)if max_profit<profit:max_pprofit = profitans = rotationprofit = waiting*boardingCost - runningCostrotation+=1if max_profit<profit:max_pprofit = profitans = rotationreturn ans if ans else -1

美化了一下代码:

profit = 0waiting = 0rotation = 0max_profit = 0ans = Nonefor customer in customers:customer += waitingrotation += 1onboarding = min(4,customer)profit += onboarding*boardingCost - runningCostwaiting = customer - onboardingif max_profit<profit:max_pprofit = profitans = rotationif 4*boardingCost - runningCost>0:steps = waiting//4profit += steps*(4*boardingCost - runningCost)waiting = waiting - steps*4if waiting*boardingCost - runningCost>0:profit += waiting*boardingCost - runningCoststeps += 1if max_profit<profit:max_pprofit = profitans = rotation + stepsreturn ans if ans else -1