欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

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

发布时间:2024/3/7 python 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Leetcode 1599. Maximum Profit of Operating a Centennial Wheel (python) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

总结

以上是生活随笔为你收集整理的Leetcode 1599. Maximum Profit of Operating a Centennial Wheel (python)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。