Leetcode 1599. Maximum Profit of Operating a Centennial Wheel (python)
生活随笔
收集整理的這篇文章主要介紹了
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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: A股中的level1跟Level2有什么
- 下一篇: 2022-2028全球及中国NTC热敏电