leetcode
leetcode 1451 ~ 1500
经营摩天轮的最大利润

经营摩天轮的最大利润

难度:

标签:

题目描述

你正在经营一座摩天轮,该摩天轮共有 4 个座舱 ,每个座舱 最多可以容纳 4 位游客 。你可以 逆时针 轮转座舱,但每次轮转都需要支付一定的运行成本 runningCost

给你一个长度为 n 的数组 customerscustomers[i] 是在第 i 次轮转(下标从 0 开始)之前到达的新游客的数量。这也意味着你必须在新游客到来前轮转 i 次。如果有座舱空闲就不能让游客等待。每位游客在登上离地面最近的座舱前都会支付登舱成本 boardingCost ,一旦该座舱再次抵达地面,他们就会离开座舱结束游玩。

你可以随时停下摩天轮,即便是 在服务所有游客之前 。如果你决定停止运营摩天轮,为了保证所有游客安全着陆,将免费进行所有后续轮转 。注意,如果有超过 4 位游客在等摩天轮,那么只有 4 位游客可以登上摩天轮,其余的需要等待 下一次轮转

返回最大化利润所需执行的 最小轮转次数 。 如果不存在利润为正的方案,则返回 -1

 

示例 1:

输入:customers = [8,3], boardingCost = 5, runningCost = 6
输出:3
解释:座舱上标注的数字是该座舱的当前游客数。
1. 8 位游客抵达,4 位登舱,4 位等待下一舱,摩天轮轮转。当前利润为 4 * $5 - 1 * $6 = $14 。
2. 3 位游客抵达,4 位在等待的游客登舱,其他 3 位等待,摩天轮轮转。当前利润为 8 * $5 - 2 * $6 = $28 。
3. 最后 3 位游客登舱,摩天轮轮转。当前利润为 11 * $5 - 3 * $6 = $37 。
轮转 3 次得到最大利润,最大利润为 $37 。

示例 2:

输入:customers = [10,9,6], boardingCost = 6, runningCost = 4
输出:7
解释:
1. 10 位游客抵达,4 位登舱,6 位等待下一舱,摩天轮轮转。当前利润为 4 * $6 - 1 * $4 = $20 。
2. 9 位游客抵达,4 位登舱,11 位等待(2 位是先前就在等待的,9 位新加入等待的),摩天轮轮转。当前利润为 8 * $6 - 2 * $4 = $40 。
3. 最后 6 位游客抵达,4 位登舱,13 位等待,摩天轮轮转。当前利润为 12 * $6 - 3 * $4 = $60 。
4. 4 位登舱,9 位等待,摩天轮轮转。当前利润为 * $6 - 4 * $4 = $80 。
5. 4 位登舱,5 位等待,摩天轮轮转。当前利润为 20 * $6 - 5 * $4 = $100 。
6. 4 位登舱,1 位等待,摩天轮轮转。当前利润为 24 * $6 - 6 * $4 = $120 。
7. 1 位登舱,摩天轮轮转。当前利润为 25 * $6 - 7 * $4 = $122 。
轮转 7 次得到最大利润,最大利润为$122 。

示例 3:

输入:customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
输出:-1
解释:
1. 3 位游客抵达,3 位登舱,0 位等待,摩天轮轮转。当前利润为 3 * $1 - 1 * $92 = -$89 。
2. 4 位游客抵达,4 位登舱,0 位等待,摩天轮轮转。当前利润为 7 * $1 - 2 * $92 = -$177 。
3. 0 位游客抵达,0 位登舱,0 位等待,摩天轮轮转。当前利润为 7 * $1 - 3 * $92 = -$269 。
4. 5 位游客抵达,4 位登舱,1 位等待,摩天轮轮转。当前利润为 11 * $1 - 4 * $92 = -$357 。
5. 1 位游客抵达,2 位登舱,0 位等待,摩天轮轮转。当前利润为 13 * $1 - 5 * $92 = -$447 。
利润永不为正,所以返回 -1 。

 

提示:

  • n == customers.length
  • 1 <= n <= 105
  • 0 <= customers[i] <= 50
  • 1 <= boardingCost, runningCost <= 100

代码结果

运行时间: 48 ms, 内存: 20.4 MB


/*
 * Approach using Java Streams:
 * 1. Use Java streams to process the customer array and calculate profits and rotations in a functional manner.
 * 2. Accumulate the total waiting customers and calculate the boarding and profit in each step.
 * 3. Track the maximum profit and corresponding number of rotations.
 */
import java.util.stream.IntStream;

public class FerrisWheelProfitStream {
    public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
        int[] maxProfitData = {0, -1};
        int[] currentWaiting = {0};
        int[] totalProfit = {0};
        int[] rotations = {0};

        IntStream.range(0, customers.length).forEach(i -> {
            currentWaiting[0] += customers[i];
            int boardingCustomers = Math.min(currentWaiting[0], 4);
            currentWaiting[0] -= boardingCustomers;
            totalProfit[0] += (boardingCustomers * boardingCost) - runningCost;
            rotations[0]++;
            if (totalProfit[0] > maxProfitData[0]) {
                maxProfitData[0] = totalProfit[0];
                maxProfitData[1] = rotations[0];
            }
        });

        while (currentWaiting[0] > 0) {
            int boardingCustomers = Math.min(currentWaiting[0], 4);
            currentWaiting[0] -= boardingCustomers;
            totalProfit[0] += (boardingCustomers * boardingCost) - runningCost;
            rotations[0]++;
            if (totalProfit[0] > maxProfitData[0]) {
                maxProfitData[0] = totalProfit[0];
                maxProfitData[1] = rotations[0];
            }
        }

        return maxProfitData[0] > 0 ? maxProfitData[1] : -1;
    }

    public static void main(String[] args) {
        FerrisWheelProfitStream solution = new FerrisWheelProfitStream();
        int[] customers = {8, 3};
        int boardingCost = 5;
        int runningCost = 6;
        System.out.println(solution.minOperationsMaxProfit(customers, boardingCost, runningCost)); // Output: 3
    }
}

解释

方法:

该题解旨在通过模拟每一轮的摩天轮运行,计算出在何时停止运行可以达到最大利润。首先判断如果每轮的最大登舱费用不能覆盖运行成本,则直接返回-1。接着,使用循环来模拟每一轮摩天轮的运行,处理每轮到达的顾客数,计算在当前轮顾客登舱后的剩余顾客数。然后更新当前的总利润,并检查是否达到了新的最大利润。循环结束时,如果存在正利润,则返回达到该利润时的最小轮次;否则返回-1。

时间复杂度:

O(n)

空间复杂度:

O(1)

代码细节讲解

🦆
在判断是否直接返回-1时,你是如何确定每轮最大的登舱费用不能覆盖运行成本就应该结束运营的?是否考虑了累积效应?
在这个算法中,我考虑的是每轮摩天轮的最大登舱收入(即4人全满时的收入)。如果这个收入乘以每人的登舱费用仍然小于或等于运行成本,那么无论客流如何变化,单轮运营都不可能实现盈利。这里没有考虑累积效应,因为累积效应指的是随时间累计而可能提高的利润,但如果单轮运营本身就不盈利,那么累计也无法覆盖持续的亏损。
🦆
在代码中,remain_customer的处理逻辑似乎有误。当remain_customer大于4时,你是如何处理超出的顾客数量?
当remain_customer大于4时,我的代码中确实有一些处理上的逻辑问题。理论上,应该将每轮最多4人登舱,超出的顾客数量应保留到下一轮。但在代码中,通过remain_customer % 4计算得到的是本轮实际登舱的顾客数,而remain_customer // 4得到的new_round用于增加已经运行的轮次。这里的处理逻辑应更清晰地反映出每轮只处理4个顾客,多余的顾客滚动到下一轮。
🦆
循环中,如果remain_customer大于4,计算new_round时使用的逻辑是否正确?为什么要将remain_customer除以4得到的值加到running_round上?
这里的逻辑确实有误。remain_customer除以4得到的是可以完整处理的轮数,而不是应该直接加到running_round上的。这种处理方式错误地假设了每轮都能处理完4个顾客,忽略了在实际情况中可能存在的顾客不足4人的情况。正确的做法应该是对于每一轮仅增加1到running_round,直到处理完所有顾客。
🦆
在更新最大利润时,你是如何确定当前轮次是达到最大利润的最小轮次的?是否有可能在后续的轮次中虽然利润没有增加但轮次数更少?
算法中通过比较当前利润与之前记录的最大利润来更新最大利润和对应的轮次。如果当前利润大于之前的最大利润,就更新最大利润并记录当前的轮次。这确保了一旦出现新的最高利润,就会记录下达到这一利润的最小轮次。后续轮次的利润如果没有超过这一最大值,则不会影响记录的轮次。这确保了只在利润增加时更新轮次,避免了您提到的情况。

相关问题