Skip to content

Commit b53835f

Browse files
authored
Create 1599-maximum-profit-of-operating-a-centennial-wheel.js
1 parent e218e97 commit b53835f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} customers
3+
* @param {number} boardingCost
4+
* @param {number} runningCost
5+
* @return {number}
6+
*/
7+
const minOperationsMaxProfit = function(customers, boardingCost, runningCost) {
8+
let remain = 0
9+
let profit = 0
10+
let cost = 0
11+
let max = -Infinity
12+
let maxNum = 0
13+
for(let i = 0, len = customers.length; i < len; i++) {
14+
const e = customers[i]
15+
remain += e
16+
const cur = (remain >= 4 ? 4 : remain)
17+
remain -= cur
18+
profit += cur * boardingCost - runningCost
19+
if(profit > max) maxNum++
20+
max = Math.max(max, profit)
21+
}
22+
if(remain) {
23+
const r = Math.floor(remain / 4)
24+
const single = 4 * boardingCost - runningCost
25+
remain = remain % 4
26+
// profit += (single * r + (remain > 0 ? (remain * boardingCost - runningCost) : 0))
27+
profit += single * r
28+
if(single > 0) maxNum += r
29+
max = Math.max(max, profit)
30+
if (remain < 4) {
31+
const tmp = remain * boardingCost - runningCost
32+
profit += tmp
33+
remain = 0
34+
if(profit > max) maxNum++
35+
max = Math.max(max, profit)
36+
}
37+
}
38+
if (max <=0 )return -1
39+
return maxNum
40+
};

0 commit comments

Comments
 (0)