Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problem: No.1599 #2170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -182,6 +182,66 @@ func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int)
}
```

### **TypeScript**

```ts
function minOperationsMaxProfit(
customers: number[],
boardingCost: number,
runningCost: number,
): number {
let ans: number = -1;
let [mx, t, wait, i] = [0, 0, 0, 0];
while (wait > 0 || i < customers.length) {
wait += i < customers.length ? customers[i] : 0;
let up: number = Math.min(4, wait);
wait -= up;
++i;
t += up * boardingCost - runningCost;

if (t > mx) {
mx = t;
ans = i;
}
}

return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32
) -> i32 {
let mut ans = -1;
let mut mx = 0;
let mut t = 0;
let mut wait = 0;
let mut i = 0;

while wait > 0 || i < customers.len() {
wait += if i < customers.len() { customers[i] } else { 0 };
let up = std::cmp::min(4, wait);
wait -= up;
i += 1;
t += up * boarding_cost - running_cost;

if t > mx {
mx = t;
ans = i as i32;
}
}

ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ The profit was never positive, so return -1.

## Solutions

**Solution 1: Simulation**

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.

The time complexity is $O(n)$, where $n$ is the length of the `customers` array. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -165,6 +171,66 @@ func minOperationsMaxProfit(customers []int, boardingCost int, runningCost int)
}
```

### **TypeScript**

```ts
function minOperationsMaxProfit(
customers: number[],
boardingCost: number,
runningCost: number,
): number {
let ans: number = -1;
let [mx, t, wait, i] = [0, 0, 0, 0];
while (wait > 0 || i < customers.length) {
wait += i < customers.length ? customers[i] : 0;
let up: number = Math.min(4, wait);
wait -= up;
++i;
t += up * boardingCost - runningCost;

if (t > mx) {
mx = t;
ans = i;
}
}

return ans;
}
```

### **Rust**

```rust
impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32
) -> i32 {
let mut ans = -1;
let mut mx = 0;
let mut t = 0;
let mut wait = 0;
let mut i = 0;

while wait > 0 || i < customers.len() {
wait += if i < customers.len() { customers[i] } else { 0 };
let up = std::cmp::min(4, wait);
wait -= up;
i += 1;
t += up * boarding_cost - running_cost;

if t > mx {
mx = t;
ans = i as i32;
}
}

ans
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
impl Solution {
pub fn min_operations_max_profit(
customers: Vec<i32>,
boarding_cost: i32,
running_cost: i32
) -> i32 {
let mut ans = -1;
let mut mx = 0;
let mut t = 0;
let mut wait = 0;
let mut i = 0;

while wait > 0 || i < customers.len() {
wait += if i < customers.len() { customers[i] } else { 0 };
let up = std::cmp::min(4, wait);
wait -= up;
i += 1;
t += up * boarding_cost - running_cost;

if t > mx {
mx = t;
ans = i as i32;
}
}

ans
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function minOperationsMaxProfit(
customers: number[],
boardingCost: number,
runningCost: number,
): number {
let ans: number = -1;
let [mx, t, wait, i] = [0, 0, 0, 0];
while (wait > 0 || i < customers.length) {
wait += i < customers.length ? customers[i] : 0;
let up: number = Math.min(4, wait);
wait -= up;
++i;
t += up * boardingCost - runningCost;

if (t > mx) {
mx = t;
ans = i;
}
}

return ans;
}