|
| 1 | +/** |
| 2 | + * 1599. Maximum Profit of Operating a Centennial Wheel |
| 3 | + * https://leetcode.com/problems/maximum-profit-of-operating-a-centennial-wheel/ |
| 4 | + * Difficulty: Medium |
| 5 | + * |
| 6 | + * You are the operator of a Centennial Wheel that has four gondolas, and each gondola has room for |
| 7 | + * up to four people. You have the ability to rotate the gondolas counterclockwise, which costs you |
| 8 | + * runningCost dollars. |
| 9 | + * |
| 10 | + * You are given an array customers of length n where customers[i] is the number of new customers |
| 11 | + * arriving just before the ith rotation (0-indexed). This means you must rotate the wheel i times |
| 12 | + * before the customers[i] customers arrive. You cannot make customers wait if there is room in the |
| 13 | + * gondola. Each customer pays boardingCost dollars when they board on the gondola closest to the |
| 14 | + * ground and will exit once that gondola reaches the ground again. |
| 15 | + * |
| 16 | + * You can stop the wheel at any time, including before serving all customers. If you decide to stop |
| 17 | + * serving customers, all subsequent rotations are free in order to get all the customers down |
| 18 | + * safely. Note that if there are currently more than four customers waiting at the wheel, only |
| 19 | + * four will board the gondola, and the rest will wait for the next rotation. |
| 20 | + * |
| 21 | + * Return the minimum number of rotations you need to perform to maximize your profit. If there is |
| 22 | + * no scenario where the profit is positive, return -1. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @param {number[]} customers |
| 27 | + * @param {number} boardingCost |
| 28 | + * @param {number} runningCost |
| 29 | + * @return {number} |
| 30 | + */ |
| 31 | +var minOperationsMaxProfit = function(customers, boardingCost, runningCost) { |
| 32 | + let waitingCustomers = 0; |
| 33 | + let totalBoarded = 0; |
| 34 | + let maxProfit = -Infinity; |
| 35 | + let rotationAtMaxProfit = -1; |
| 36 | + let currentProfit = 0; |
| 37 | + let rotations = 0; |
| 38 | + |
| 39 | + const gondolaCapacity = 4; |
| 40 | + |
| 41 | + for (let i = 0; i < customers.length || waitingCustomers > 0; i++) { |
| 42 | + rotations++; |
| 43 | + |
| 44 | + if (i < customers.length) { |
| 45 | + waitingCustomers += customers[i]; |
| 46 | + } |
| 47 | + |
| 48 | + const boarding = Math.min(waitingCustomers, gondolaCapacity); |
| 49 | + waitingCustomers -= boarding; |
| 50 | + totalBoarded += boarding; |
| 51 | + |
| 52 | + currentProfit = totalBoarded * boardingCost - rotations * runningCost; |
| 53 | + |
| 54 | + if (currentProfit > maxProfit) { |
| 55 | + maxProfit = currentProfit; |
| 56 | + rotationAtMaxProfit = rotations; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return maxProfit > 0 ? rotationAtMaxProfit : -1; |
| 61 | +}; |
0 commit comments