跳到主要内容

1599.经营摩天轮的最大利润

链接:1599.经营摩天轮的最大利润
难度:Medium
标签:数组、模拟
简介:返回最大化利润所需执行的 最小轮转次数 。

题解 1 - cpp

  • 编辑时间:2023-03-05
  • 执行用时:100ms
  • 内存消耗:79.5MB
  • 编程语言:cpp
  • 解法介绍:模拟。
class Solution {
public:
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
int resMax = 0, resCnt = -1, wait = 0, cur = 0, i = 0;
while (wait != 0 || i < customers.size()) {
if (i < customers.size()) wait += customers[i];
cur += min(wait, 4) * boardingCost - runningCost;
wait = max(wait - 4, 0);
if (cur > resMax) resMax = cur, resCnt = i + 1;
i += 1;
}
return resCnt;
}
};

题解 2 - python

  • 编辑时间:2023-03-05
  • 执行用时:1656ms
  • 内存消耗:19.1MB
  • 编程语言:python
  • 解法介绍:同上。
class Solution:
def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:
resMax, resCnt = 0, -1
wait, cur, i = 0, 0, 0
while wait != 0 or i < len(customers):
if i < len(customers):
wait += customers[i]
cur += min(wait, 4) * boardingCost - runningCost
wait = max(wait - 4, 0)
if cur > resMax:
resMax = cur
resCnt = i + 1
i += 1
return resCnt

题解 3 - rust

  • 编辑时间:2023-03-05
  • 执行用时:24ms
  • 内存消耗:2.8MB
  • 编程语言:rust
  • 解法介绍:同上。
impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32,
) -> i32 {
let (mut resMax, mut resCnt, mut wait, mut cur, mut i) = (0, -1, 0, 0, 0);
while wait != 0 || i < customers.len() {
if i < customers.len() {
wait += customers[i];
}
cur += wait.min(4) * boarding_cost - running_cost;
wait = 0.max(wait - 4);
if cur > resMax {
resMax = cur;
resCnt = i as i32 + 1;
}
i += 1;
}
resCnt
}
}

题解 4 - python

  • 编辑时间:2024-01-01
  • 执行用时:1200ms
  • 内存消耗:20.89MB
  • 编程语言:python
  • 解法介绍:模拟。
class Solution:
def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int:
costCnt = costSum = resSum =0
resCnt = -1
mask = 0b0000
wait = 0

def walk():
nonlocal costCnt, costSum, resSum, resCnt, mask, wait
cnt = min(wait, 4)
costSum += cnt * boardingCost - runningCost
wait -= cnt
costCnt += 1
mask = ((mask << 1) | 0b0001) & 0b1111
if costSum > resSum:
resCnt, resSum = costCnt, costSum
for v in customers:
wait += v
walk()
while wait: walk()
return resCnt