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

每次从 customers 拿到新到的顾客数量放到 waiting 缓冲区中,然后我们从 waiting 缓冲区中至多拿 4 个来计算收益,如果当前收益超过过去的最大收益则记录下收益和当前圈数


impl Solution {
    pub fn min_operations_max_profit(
        mut customers: Vec<i32>,
        boarding_cost: i32,
        running_cost: i32,
    ) -> i32 {
        let mut waiting = customers.remove(0);
        let mut max_profit = 0;
        let mut max_rotate = 0;
        let mut curr = 0;
        let mut rotates = 0;
        while waiting > 0 || !customers.is_empty() {
            let actual = waiting.clamp(0, 4);
            waiting -= actual;
            curr += boarding_cost * actual - running_cost;
            rotates += 1;
            if curr > max_profit {
                max_profit = curr;
                max_rotate = rotates;
            }
            waiting += if !customers.is_empty() {
                customers.remove(0)
            } else {
                0
            }
        }
        if max_rotate == 0 {
            -1
        } else {
            max_rotate
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值