1+ public class Solution1599 {
2+ public int minOperationsMaxProfit (int [] customers , int boardingCost , int runningCost ) {
3+ // 最大化利润所需执行的 最小轮转次数
4+ int res = -1 ;
5+
6+ // 轮转数,抵达的游客数,当前收益,最大收益
7+ int rotations = 0 ;
8+ int customerCnt = 0 ;
9+ int curProfit = 0 ;
10+ int maxProfit = 0 ;
11+
12+ for (int customer : customers ) {
13+ rotations ++;
14+ customerCnt += customer ;
15+ // 这一轮上 k 个人
16+ int k = Math .min (4 , customerCnt );
17+ customerCnt -= k ;
18+ curProfit += boardingCost * k - runningCost ;
19+
20+ if (maxProfit < curProfit ) {
21+ maxProfit = curProfit ;
22+ res = rotations ;
23+ }
24+ }
25+ if (customerCnt == 0 ) {
26+ return res ;
27+ }
28+
29+ // 如果还有剩余的游客在等摩天轮,只有当剩余的游客带来的利润为正时,才需要考虑第二阶段
30+ int once = boardingCost * 4 - runningCost ;
31+ if (once <= 0 ) {
32+ return res ;
33+ }
34+ // 剩余轮数
35+ int remainRotations = customerCnt / 4 ;
36+ curProfit += remainRotations * once ;
37+ rotations += remainRotations ;
38+ if (maxProfit < curProfit ) {
39+ maxProfit = curProfit ;
40+ res = rotations ;
41+ }
42+ // 剩余游客
43+ int remainCustomers = customerCnt % 4 ;
44+ curProfit += boardingCost * remainCustomers - runningCost ;
45+ if (maxProfit < curProfit ) {
46+ rotations ++;
47+ maxProfit = curProfit ;
48+ res = rotations ;
49+ }
50+ return res ;
51+ }
52+ }
53+ /*
54+ 1599. 经营摩天轮的最大利润
55+ https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel/
56+
57+ 你正在经营一座摩天轮,该摩天轮共有 4 个座舱 ,每个座舱 最多可以容纳 4 位游客 。
58+ 你可以 逆时针 轮转座舱,但每次轮转都需要支付一定的运行成本 runningCost 。摩天轮每次轮转都恰好转动 1 / 4 周。
59+ 给你一个长度为 n 的数组 customers , customers[i] 是在第 i 次轮转(下标从 0 开始)之前到达的新游客的数量。这也意味着你必须在新游客到来前轮转 i 次。
60+ 每位游客在登上离地面最近的座舱前都会支付登舱成本 boardingCost ,一旦该座舱再次抵达地面,他们就会离开座舱结束游玩。
61+ 你可以随时停下摩天轮,即便是 在服务所有游客之前 。如果你决定停止运营摩天轮,为了保证所有游客安全着陆,将免费进行所有后续轮转 。
62+ 注意,如果有超过 4 位游客在等摩天轮,那么只有 4 位游客可以登上摩天轮,其余的需要等待 下一次轮转 。
63+ 返回最大化利润所需执行的 最小轮转次数 。 如果不存在利润为正的方案,则返回 -1 。
64+ 提示:
65+ n == customers.length
66+ 1 <= n <= 10^5
67+ 0 <= customers[i] <= 50
68+ 1 <= boardingCost, runningCost <= 100
69+
70+ 模拟
71+ */
0 commit comments