重庆网购平台/重庆seo技术教程
题目:原题链接(中等)
标签:贪心算法
解法 | 时间复杂度 | 空间复杂度 | 执行用时 |
---|---|---|---|
Ans 1 (Python) | O(C)O(C)O(C) : 其中C为顾客总数 | O(1)O(1)O(1) | 2056ms (29%) |
Ans 2 (Python) | |||
Ans 3 (Python) |
解法一(情景模拟):
class Solution:def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:max_val = 0max_step = -1money = 0wait = 0i = 0while i < len(customers) or wait > 0:people = customers[i] if i < len(customers) else 0money -= runningCostwait += peopleif wait >= 4:money += boardingCost * 4wait -= 4if money > max_val:max_val = moneymax_step = i + 1elif wait > 0:money += boardingCost * waitwait = 0if money > max_val:max_val = moneymax_step = i + 1i += 1return max_step