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

    科技2025-07-06  16

    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 = 0 waiting = 0 rotation = 0 max_profit = 0 ans = None for customer in customers: customer += waiting rotation += 1 if customer>=4: profit += 4*boardingCost - runningCost waiting = customer-4 else: profit = customer*boardingCost - runningCost waiting = 0 if max_profit<profit: max_pprofit = profit ans = rotation if waiting>0: if waiting>4: while waiting>4: profit += 4*boardingCost - runningCost waiting = waiting-4 rotation += 1 #print(profit) if max_profit<profit: max_pprofit = profit ans = rotation profit = waiting*boardingCost - runningCost rotation+=1 if max_profit<profit: max_pprofit = profit ans = rotation return ans if ans else -1

    美化了一下代码:

    profit = 0 waiting = 0 rotation = 0 max_profit = 0 ans = None for customer in customers: customer += waiting rotation += 1 onboarding = min(4,customer) profit += onboarding*boardingCost - runningCost waiting = customer - onboarding if max_profit<profit: max_pprofit = profit ans = rotation if 4*boardingCost - runningCost>0: steps = waiting//4 profit += steps*(4*boardingCost - runningCost) waiting = waiting - steps*4 if waiting*boardingCost - runningCost>0: profit += waiting*boardingCost - runningCost steps += 1 if max_profit<profit: max_pprofit = profit ans = rotation + steps return ans if ans else -1
    Processed: 0.013, SQL: 8