[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 the customers[i] customers arrive. You cannot make customers wait if there is room in the gondola. 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 11 * $1 - 4 * $92 = -$357.
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.

Constraints:

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

经营摩天轮的最大利润。

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

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是贪心。题意是有一个摩天轮,有 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 题目总结

posted @ 2020-09-27 13:08  CNoodle  阅读(277)  评论(0编辑  收藏  举报