Skip to content

Commit f10e4c9

Browse files
authored
feat: add solutions to lc problem: No.1599 (#2170)
No.1599.Maximum Profit of Operating a Centennial Wheel
1 parent 2604e22 commit f10e4c9

File tree

4 files changed

+177
-1
lines changed

4 files changed

+177
-1
lines changed

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

+61-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
我们直接模拟摩天轮的轮转过程,每次轮转时,累加等待的游客以及新到达的游客,然后最多 $4$ 个人上船,更新等待的游客数和利润,记录最大利润与其对应的轮转次数。
8080

81-
时间复杂度 $O(\frac{S}{4})$,其中 $S$ 为数组 `customers` 的元素和,即游客总数。空间复杂度 $O(1)$。
81+
时间复杂度 $O(n)$,其中 $n$ 为数组 `customers` 的长度。空间复杂度 $O(1)$。
8282

8383
<!-- tabs:start -->
8484

@@ -182,6 +182,66 @@ func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int)
182182
}
183183
```
184184

185+
### **TypeScript**
186+
187+
```ts
188+
function minOperationsMaxProfit(
189+
customers: number[],
190+
boardingCost: number,
191+
runningCost: number,
192+
): number {
193+
let ans: number = -1;
194+
let [mx, t, wait, i] = [0, 0, 0, 0];
195+
while (wait > 0 || i < customers.length) {
196+
wait += i < customers.length ? customers[i] : 0;
197+
let up: number = Math.min(4, wait);
198+
wait -= up;
199+
++i;
200+
t += up * boardingCost - runningCost;
201+
202+
if (t > mx) {
203+
mx = t;
204+
ans = i;
205+
}
206+
}
207+
208+
return ans;
209+
}
210+
```
211+
212+
### **Rust**
213+
214+
```rust
215+
impl Solution {
216+
pub fn min_operations_max_profit(
217+
customers: Vec<i32>,
218+
boarding_cost: i32,
219+
running_cost: i32
220+
) -> i32 {
221+
let mut ans = -1;
222+
let mut mx = 0;
223+
let mut t = 0;
224+
let mut wait = 0;
225+
let mut i = 0;
226+
227+
while wait > 0 || i < customers.len() {
228+
wait += if i < customers.len() { customers[i] } else { 0 };
229+
let up = std::cmp::min(4, wait);
230+
wait -= up;
231+
i += 1;
232+
t += up * boarding_cost - running_cost;
233+
234+
if t > mx {
235+
mx = t;
236+
ans = i as i32;
237+
}
238+
}
239+
240+
ans
241+
}
242+
}
243+
```
244+
185245
### **...**
186246

187247
```

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

+66
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ The profit was never positive, so return -1.
6767

6868
## Solutions
6969

70+
**Solution 1: Simulation**
71+
72+
We directly simulate the rotation process of the Ferris wheel. Each time it rotates, we add up the waiting customers and the newly arrived customers, then at most $4$ people get on the ride, update the number of waiting customers and profit, and record the maximum profit and its corresponding number of rotations.
73+
74+
The time complexity is $O(n)$, where $n$ is the length of the `customers` array. The space complexity is $O(1)$.
75+
7076
<!-- tabs:start -->
7177

7278
### **Python3**
@@ -165,6 +171,66 @@ func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int)
165171
}
166172
```
167173

174+
### **TypeScript**
175+
176+
```ts
177+
function minOperationsMaxProfit(
178+
customers: number[],
179+
boardingCost: number,
180+
runningCost: number,
181+
): number {
182+
let ans: number = -1;
183+
let [mx, t, wait, i] = [0, 0, 0, 0];
184+
while (wait > 0 || i < customers.length) {
185+
wait += i < customers.length ? customers[i] : 0;
186+
let up: number = Math.min(4, wait);
187+
wait -= up;
188+
++i;
189+
t += up * boardingCost - runningCost;
190+
191+
if (t > mx) {
192+
mx = t;
193+
ans = i;
194+
}
195+
}
196+
197+
return ans;
198+
}
199+
```
200+
201+
### **Rust**
202+
203+
```rust
204+
impl Solution {
205+
pub fn min_operations_max_profit(
206+
customers: Vec<i32>,
207+
boarding_cost: i32,
208+
running_cost: i32
209+
) -> i32 {
210+
let mut ans = -1;
211+
let mut mx = 0;
212+
let mut t = 0;
213+
let mut wait = 0;
214+
let mut i = 0;
215+
216+
while wait > 0 || i < customers.len() {
217+
wait += if i < customers.len() { customers[i] } else { 0 };
218+
let up = std::cmp::min(4, wait);
219+
wait -= up;
220+
i += 1;
221+
t += up * boarding_cost - running_cost;
222+
223+
if t > mx {
224+
mx = t;
225+
ans = i as i32;
226+
}
227+
}
228+
229+
ans
230+
}
231+
}
232+
```
233+
168234
### **...**
169235

170236
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn min_operations_max_profit(
3+
customers: Vec<i32>,
4+
boarding_cost: i32,
5+
running_cost: i32
6+
) -> i32 {
7+
let mut ans = -1;
8+
let mut mx = 0;
9+
let mut t = 0;
10+
let mut wait = 0;
11+
let mut i = 0;
12+
13+
while wait > 0 || i < customers.len() {
14+
wait += if i < customers.len() { customers[i] } else { 0 };
15+
let up = std::cmp::min(4, wait);
16+
wait -= up;
17+
i += 1;
18+
t += up * boarding_cost - running_cost;
19+
20+
if t > mx {
21+
mx = t;
22+
ans = i as i32;
23+
}
24+
}
25+
26+
ans
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function minOperationsMaxProfit(
2+
customers: number[],
3+
boardingCost: number,
4+
runningCost: number,
5+
): number {
6+
let ans: number = -1;
7+
let [mx, t, wait, i] = [0, 0, 0, 0];
8+
while (wait > 0 || i < customers.length) {
9+
wait += i < customers.length ? customers[i] : 0;
10+
let up: number = Math.min(4, wait);
11+
wait -= up;
12+
++i;
13+
t += up * boardingCost - runningCost;
14+
15+
if (t > mx) {
16+
mx = t;
17+
ans = i;
18+
}
19+
}
20+
21+
return ans;
22+
}

0 commit comments

Comments
 (0)