Skip to content

Commit ebe3ef7

Browse files
authored
Create maximum-profit-of-operating-a-centennial-wheel.cpp
1 parent c9e2566 commit ebe3ef7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
7+
int max_run = -1;
8+
for (int i = 0, prof = 0, run = 1, waiting = 0, max_prof = 0;
9+
i < size(customers) || waiting > 0; ++run) {
10+
if (i < size(customers)) {
11+
waiting += customers[i++]; // each run i increases people by customers[i]
12+
}
13+
int boarding = min(waiting, 4); // greedy
14+
waiting -= boarding;
15+
prof += boarding * boardingCost - runningCost;
16+
if (prof > max_prof) {
17+
max_prof = prof;
18+
max_run = run;
19+
}
20+
}
21+
return max_run;
22+
}
23+
};

0 commit comments

Comments
 (0)