diff --git a/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README.md b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README.md index e1641c5e6064e..aaf8bae2442e3 100644 --- a/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README.md +++ b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README.md @@ -78,7 +78,7 @@ 我们直接模拟摩天轮的轮转过程,每次轮转时,累加等待的游客以及新到达的游客,然后最多 $4$ 个人上船,更新等待的游客数和利润,记录最大利润与其对应的轮转次数。 -时间复杂度 $O(\frac{S}{4})$,其中 $S$ 为数组 `customers` 的元素和,即游客总数。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 `customers` 的长度。空间复杂度 $O(1)$。 @@ -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, + 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 + } +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README_EN.md b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README_EN.md index efe07c0b1ad1f..a2e52e1701e86 100644 --- a/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README_EN.md +++ b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/README_EN.md @@ -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)$. + ### **Python3** @@ -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, + 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 + } +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/Solution.rs b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/Solution.rs new file mode 100644 index 0000000000000..ed9d48f032cab --- /dev/null +++ b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/Solution.rs @@ -0,0 +1,28 @@ +impl Solution { + pub fn min_operations_max_profit( + customers: Vec, + 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 + } +} diff --git a/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/Solution.ts b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/Solution.ts new file mode 100644 index 0000000000000..ae76bdbcb9705 --- /dev/null +++ b/solution/1500-1599/1599.Maximum Profit of Operating a Centennial Wheel/Solution.ts @@ -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; +}