zoukankan      html  css  js  c++  java
  • [LeetCode] 1599. Maximum Profit of Operating a Centennial Wheel

    You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you runningCost dollars.

    You are given an array customers of length n where customers[i] is the number of new customers arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times before customers[i] arrive. Each customer pays boardingCost dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again.

    You can stop the wheel at any time, including before serving all customers. If you decide to stop serving customers, all subsequent rotations are free in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait for the next rotation.

    Return the minimum number of rotations you need to perform to maximize your profit. If there is no scenario where the profit is positive, return -1.

    Example 1:

    Input: customers = [8,3], boardingCost = 5, runningCost = 6
    Output: 3
    Explanation: The numbers written on the gondolas are the number of people currently there.
    1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14.
    2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28.
    3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37.
    The highest profit was $37 after rotating the wheel 3 times.

    Example 2:

    Input: customers = [10,9,6], boardingCost = 6, runningCost = 4
    Output: 7
    Explanation:
    1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20.
    2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40.
    3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60.
    4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80.
    5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100.
    6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120.
    7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122.
    The highest profit was $122 after rotating the wheel 7 times.
    
    

    Example 3:

    Input: customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92
    Output: -1
    Explanation:
    1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89.
    2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177.
    3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269.
    4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 12 * $1 - 4 * $92 = -$356.
    5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447.
    The profit was never positive, so return -1.
    

    Example 4:

    Input: customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8
    Output: 9
    Explanation:
    1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4.
    2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8.
    3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12.
    4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16.
    5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20.
    6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24.
    7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28.
    8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32.
    9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36.
    10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31.
    The highest profit was $36 after rotating the wheel 9 times.

    Constraints:

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

    经营摩天轮的最大利润。题意解释的有点复杂,我这里稍作简化。题意是有一个摩天轮,有4组座位,每组座位可以坐4位乘客。摩天轮每次转动四分之一圈,在底部的那一组座位可供乘客的上下。现在给你一个customers数组,里面包含每一个时刻 i 来试图乘坐摩天轮的乘客数量customers[i];同时也给了乘客的票价是boardingCost/人和摩天轮的运营成本runningCost。这个runningCost是指摩天轮每转动四分之一圈的花费。你只能在 i 时刻安排customers[i]去坐摩天轮,不能提早。但是如果摩天轮上没有座位,可以让这些乘客等待。返回最大化利润所需执行的最小轮转次数。如果不存在利润为正的方案,则返回 -1。

    这道题涉及一点点的贪心,但是更多的是一个实现题。既然问的是经营摩天轮的最大利润,比较粗暴的想法就是看看能否尽量每次都让4位乘客坐上去,这样每转动四分之一圈,收益是最大的。遍历input数组,在累加乘客总人数的同时,开始将乘客往摩天轮上放。每次可以坐上摩天轮的乘客人数是boarding = Math.min(4, sum)。这些乘客坐上去之后,同时有几个动作要做

    • 总人数total - Math.min(4, sum)
    • 利润profit = profit + boarding * boardingCost - runningCost
    • 轮转次数++

    如果此时的利润profit最大,则记录一下当前的轮数。循环这些动作,直到乘客人数小于0或者customers数组遍历完毕。最后如果最大利润 > 0则返回轮转次数,否则返回-1。

    时间O(n)

    空间O(1)

    Java实现

     1 class Solution {
     2     public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
     3         int run = 0;
     4         int maxRun = 1;
     5         int prof = 0;
     6         int maxProf = prof;
     7         int sum = 0;
     8         int i = 0;
     9         while (sum > 0 || i < customers.length) {
    10             if (i < customers.length) {
    11                 sum += customers[i++];
    12             }
    13             int boarding = Math.min(4, sum);  // boarding people by greedy. 
    14             sum -= boarding;
    15             prof = prof + boarding * boardingCost - runningCost;
    16             run++;
    17             if (prof > maxProf) {
    18                 maxProf = prof;
    19                 maxRun = run;
    20             }
    21         }
    22         return maxProf > 0 ? maxRun : -1;
    23     }
    24 }

    LeetCode 题目总结

  • 相关阅读:
    ireport +jasperreport 中文不能显示
    ireport +jasperreport 中文不能显示
    JDBC的批量处理语句
    tattoo实现adhoc连接
    tattoo实现adhoc连接
    网络突然掉线或者不小心putty被关掉等等原因 造成安装过程被中断怎么办?
    网络突然掉线或者不小心putty被关掉等等原因 造成安装过程被中断怎么办?
    Linux SSH命令大全,新手必看SSH命令
    修改SSH端口和修改vsftpd端口更安全
    Linux SSH命令大全,新手必看SSH命令
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13738854.html
Copyright © 2011-2022 走看看