Skip to content

Commit d3bc03c

Browse files
committed
feat: add solutions to lc problem: No.1599
No.1599.Maximum Profit of Operating a Centennial Wheel
1 parent bee9c54 commit d3bc03c

File tree

6 files changed

+273
-2
lines changed

6 files changed

+273
-2
lines changed

solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,119 @@
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77+
**方法一:模拟**
78+
79+
直接模拟摩天轮的轮转过程,每次轮转时,累加等待的游客以及新到达的游客,然后最多 4 个人上船,更新等待的游客数和利润,记录最大利润与其对应的轮转次数。
80+
81+
时间复杂度 $O(n)$。其中 $n$ 为数组 `customers` 的长度。
82+
7783
<!-- tabs:start -->
7884

7985
### **Python3**
8086

8187
<!-- 这里可写当前语言的特殊实现逻辑 -->
8288

8389
```python
84-
90+
class Solution:
91+
def minOperationsMaxProfit(
92+
self, customers: List[int], boardingCost: int, runningCost: int
93+
) -> int:
94+
ans = -1
95+
mx = t = 0
96+
wait = 0
97+
i = 0
98+
while wait or i < len(customers):
99+
wait += customers[i] if i < len(customers) else 0
100+
up = min(wait, 4)
101+
wait -= up
102+
t += up * boardingCost - runningCost
103+
i += 1
104+
if t > mx:
105+
mx = t
106+
ans = i
107+
return ans
85108
```
86109

87110
### **Java**
88111

89112
<!-- 这里可写当前语言的特殊实现逻辑 -->
90113

91114
```java
115+
class Solution {
116+
public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
117+
int ans = -1;
118+
int mx = 0, t = 0;
119+
int wait = 0, i = 0;
120+
while (wait > 0 || i < customers.length) {
121+
wait += i < customers.length ? customers[i] : 0;
122+
int up = Math.min(4, wait);
123+
wait -= up;
124+
++i;
125+
t += up * boardingCost - runningCost;
126+
if (t > mx) {
127+
mx = t;
128+
ans = i;
129+
}
130+
}
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
142+
int ans = -1;
143+
int mx = 0, t = 0;
144+
int wait = 0, i = 0;
145+
while (wait || i < customers.size()) {
146+
wait += i < customers.size() ? customers[i] : 0;
147+
int up = min(4, wait);
148+
wait -= up;
149+
++i;
150+
t += up * boardingCost - runningCost;
151+
if (t > mx) {
152+
t = mx;
153+
ans = i;
154+
}
155+
}
156+
return ans;
157+
}
158+
};
159+
```
92160
161+
### **Go**
162+
163+
```go
164+
func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int) int {
165+
ans := -1
166+
t, mx := 0, 0
167+
wait, i := 0, 0
168+
for wait > 0 || i < len(customers) {
169+
if i < len(customers) {
170+
wait += customers[i]
171+
}
172+
up := min(4, wait)
173+
wait -= up
174+
t += up*boardingCost - runningCost
175+
i++
176+
if t > mx {
177+
mx = t
178+
ans = i
179+
}
180+
}
181+
return ans
182+
}
183+
184+
func min(a, b int) int {
185+
if a < b {
186+
return a
187+
}
188+
return b
189+
}
93190
```
94191

95192
### **...**

solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README_EN.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,104 @@ The profit was never positive, so return -1.
7272
### **Python3**
7373

7474
```python
75-
75+
class Solution:
76+
def minOperationsMaxProfit(
77+
self, customers: List[int], boardingCost: int, runningCost: int
78+
) -> int:
79+
ans = -1
80+
mx = t = 0
81+
wait = 0
82+
i = 0
83+
while wait or i < len(customers):
84+
wait += customers[i] if i < len(customers) else 0
85+
up = min(wait, 4)
86+
wait -= up
87+
t += up * boardingCost - runningCost
88+
i += 1
89+
if t > mx:
90+
mx = t
91+
ans = i
92+
return ans
7693
```
7794

7895
### **Java**
7996

8097
```java
98+
class Solution {
99+
public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
100+
int ans = -1;
101+
int mx = 0, t = 0;
102+
int wait = 0, i = 0;
103+
while (wait > 0 || i < customers.length) {
104+
wait += i < customers.length ? customers[i] : 0;
105+
int up = Math.min(4, wait);
106+
wait -= up;
107+
++i;
108+
t += up * boardingCost - runningCost;
109+
if (t > mx) {
110+
mx = t;
111+
ans = i;
112+
}
113+
}
114+
return ans;
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
125+
int ans = -1;
126+
int mx = 0, t = 0;
127+
int wait = 0, i = 0;
128+
while (wait || i < customers.size()) {
129+
wait += i < customers.size() ? customers[i] : 0;
130+
int up = min(4, wait);
131+
wait -= up;
132+
++i;
133+
t += up * boardingCost - runningCost;
134+
if (t > mx) {
135+
t = mx;
136+
ans = i;
137+
}
138+
}
139+
return ans;
140+
}
141+
};
142+
```
81143
144+
### **Go**
145+
146+
```go
147+
func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int) int {
148+
ans := -1
149+
t, mx := 0, 0
150+
wait, i := 0, 0
151+
for wait > 0 || i < len(customers) {
152+
if i < len(customers) {
153+
wait += customers[i]
154+
}
155+
up := min(4, wait)
156+
wait -= up
157+
t += up*boardingCost - runningCost
158+
i++
159+
if t > mx {
160+
mx = t
161+
ans = i
162+
}
163+
}
164+
return ans
165+
}
166+
167+
func min(a, b int) int {
168+
if a < b {
169+
return a
170+
}
171+
return b
172+
}
82173
```
83174

84175
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
4+
int ans = -1;
5+
int mx = 0, t = 0;
6+
int wait = 0, i = 0;
7+
while (wait || i < customers.size()) {
8+
wait += i < customers.size() ? customers[i] : 0;
9+
int up = min(4, wait);
10+
wait -= up;
11+
++i;
12+
t += up * boardingCost - runningCost;
13+
if (t > mx) {
14+
t = mx;
15+
ans = i;
16+
}
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int) int {
2+
ans := -1
3+
t, mx := 0, 0
4+
wait, i := 0, 0
5+
for wait > 0 || i < len(customers) {
6+
if i < len(customers) {
7+
wait += customers[i]
8+
}
9+
up := min(4, wait)
10+
wait -= up
11+
t += up*boardingCost - runningCost
12+
i++
13+
if t > mx {
14+
mx = t
15+
ans = i
16+
}
17+
}
18+
return ans
19+
}
20+
21+
func min(a, b int) int {
22+
if a < b {
23+
return a
24+
}
25+
return b
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
3+
int ans = -1;
4+
int mx = 0, t = 0;
5+
int wait = 0, i = 0;
6+
while (wait > 0 || i < customers.length) {
7+
wait += i < customers.length ? customers[i] : 0;
8+
int up = Math.min(4, wait);
9+
wait -= up;
10+
++i;
11+
t += up * boardingCost - runningCost;
12+
if (t > mx) {
13+
mx = t;
14+
ans = i;
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def minOperationsMaxProfit(
3+
self, customers: List[int], boardingCost: int, runningCost: int
4+
) -> int:
5+
ans = -1
6+
mx = t = 0
7+
wait = 0
8+
i = 0
9+
while wait or i < len(customers):
10+
wait += customers[i] if i < len(customers) else 0
11+
up = min(wait, 4)
12+
wait -= up
13+
t += up * boardingCost - runningCost
14+
i += 1
15+
if t > mx:
16+
mx = t
17+
ans = i
18+
return ans

0 commit comments

Comments
 (0)