Skip to content

Commit 239e780

Browse files
committed
Add solution #1599
1 parent 0b7fef9 commit 239e780

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,409 LeetCode solutions in JavaScript
1+
# 1,410 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1234,6 +1234,7 @@
12341234
1594|[Maximum Non Negative Product in a Matrix](./solutions/1594-maximum-non-negative-product-in-a-matrix.js)|Medium|
12351235
1595|[Minimum Cost to Connect Two Groups of Points](./solutions/1595-minimum-cost-to-connect-two-groups-of-points.js)|Hard|
12361236
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
1237+
1599|[Maximum Profit of Operating a Centennial Wheel](./solutions/1599-maximum-profit-of-operating-a-centennial-wheel.js)|Medium|
12371238
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12381239
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12391240
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)