From 9262ecbcf03da9f741b6666ecfb83b1c96e7638c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 19 May 2024 20:51:44 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.3155 No.3155.Maximum Number of Upgradable Servers --- .../3152.Special Array II/README_EN.md | 8 +- .../README.md | 157 ++++++++++++++++++ .../README_EN.md | 157 ++++++++++++++++++ .../Solution.cpp | 11 ++ .../Solution.go | 6 + .../Solution.java | 10 ++ .../Solution.py | 8 + .../Solution.ts | 14 ++ solution/README.md | 1 + solution/README_EN.md | 1 + 10 files changed, 369 insertions(+), 4 deletions(-) create mode 100644 solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md create mode 100644 solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md create mode 100644 solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.cpp create mode 100644 solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.go create mode 100644 solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java create mode 100644 solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.py create mode 100644 solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.ts diff --git a/solution/3100-3199/3152.Special Array II/README_EN.md b/solution/3100-3199/3152.Special Array II/README_EN.md index 96f22fb8c1ff6..f01a74821d386 100644 --- a/solution/3100-3199/3152.Special Array II/README_EN.md +++ b/solution/3100-3199/3152.Special Array II/README_EN.md @@ -61,6 +61,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3152.Sp +## Solutions + + + ### Solution 1: Record the Leftmost Special Array Position for Each Position We can define an array $d$ to record the leftmost special array position for each position, initially $d[i] = i$. Then we traverse the array $nums$ from left to right. If $nums[i]$ and $nums[i - 1]$ have different parities, then $d[i] = d[i - 1]$. @@ -69,10 +73,6 @@ Finally, we traverse each query and check whether $d[to] <= from$ holds. The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array. - - -### Solution 1 - #### Python3 diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md new file mode 100644 index 0000000000000..f6e1f6a029568 --- /dev/null +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md @@ -0,0 +1,157 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md +--- + + + +# [3155. Maximum Number of Upgradable Servers 🔒](https://leetcode.cn/problems/maximum-number-of-upgradable-servers) + +[English Version](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md) + +## 题目描述 + + + +

You have n data centers and need to upgrade their servers.

+ +

You are given four arrays count, upgrade, sell, and money of length n, which show:

+ + + +

for each data center respectively.

+ +

Return an array answer, where for each data center, the corresponding element in answer represents the maximum number of servers that can be upgraded.

+ +

Note that the money from one data center cannot be used for another data center.

+ +

 

+

Example 1:

+ +
+

Input: count = [4,3], upgrade = [3,5], sell = [4,2], money = [8,9]

+ +

Output: [3,2]

+ +

Explanation:

+ +

For the first data center, if we sell one server, we'll have 8 + 4 = 12 units of money and we can upgrade the remaining 3 servers.

+ +

For the second data center, if we sell one server, we'll have 9 + 2 = 11 units of money and we can upgrade the remaining 2 servers.

+
+ +

Example 2:

+ +
+

Input: count = [1], upgrade = [2], sell = [1], money = [1]

+ +

Output: [0]

+
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:数学 + +对于每个数据中心,我们假设可以升级 $\text{x}$ 台服务器,那么 $\text{x} \times \text{upgrade[i]} \leq \text{count[i]} \times \text{sell[i]} + \text{money[i]}$。即 $\text{x} \leq \frac{\text{count[i]} \times \text{sell[i]} + \text{money[i]}}{\text{upgrade[i]} + \text{sell[i]}}$。又因为 $\text{x} \leq \text{count[i]}$,所以我们取两者的最小值即可。 + +时间复杂度 $O(n)$,其中 $n$ 为数组的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。 + + + +#### Python3 + +```python +class Solution: + def maxUpgrades( + self, count: List[int], upgrade: List[int], sell: List[int], money: List[int] + ) -> List[int]: + ans = [] + for cnt, cost, income, cash in zip(count, upgrade, sell, money): + ans.append(min(cnt, (cnt * income + cash) // (cost + income))) + return ans +``` + +#### Java + +```java +class Solution { + public int[] maxUpgrades(int[] count, int[] upgrade, int[] sell, int[] money) { + int n = count.length; + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = Math.min(count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector maxUpgrades(vector& count, vector& upgrade, vector& sell, vector& money) { + int n = count.size(); + vector ans; + for (int i = 0; i < n; ++i) { + ans.push_back(min(count[i], (int) ((1LL * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])))); + } + return ans; + } +}; +``` + +#### Go + +```go +func maxUpgrades(count []int, upgrade []int, sell []int, money []int) (ans []int) { + for i, cnt := range count { + ans = append(ans, min(cnt, (cnt*sell[i]+money[i])/(upgrade[i]+sell[i]))) + } + return +} +``` + +#### TypeScript + +```ts +function maxUpgrades( + count: number[], + upgrade: number[], + sell: number[], + money: number[], +): number[] { + const n = count.length; + const ans: number[] = []; + for (let i = 0; i < n; ++i) { + const x = ((count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])) | 0; + ans.push(Math.min(x, count[i])); + } + return ans; +} +``` + + + + + + diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md new file mode 100644 index 0000000000000..24d0c25ac9c32 --- /dev/null +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md @@ -0,0 +1,157 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md +--- + + + +# [3155. Maximum Number of Upgradable Servers 🔒](https://leetcode.com/problems/maximum-number-of-upgradable-servers) + +[中文文档](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md) + +## Description + + + +

You have n data centers and need to upgrade their servers.

+ +

You are given four arrays count, upgrade, sell, and money of length n, which show:

+ +
    +
  • The number of servers
  • +
  • The cost of upgrading a single server
  • +
  • The money you get by selling a server
  • +
  • The money you initially have
  • +
+ +

for each data center respectively.

+ +

Return an array answer, where for each data center, the corresponding element in answer represents the maximum number of servers that can be upgraded.

+ +

Note that the money from one data center cannot be used for another data center.

+ +

 

+

Example 1:

+ +
+

Input: count = [4,3], upgrade = [3,5], sell = [4,2], money = [8,9]

+ +

Output: [3,2]

+ +

Explanation:

+ +

For the first data center, if we sell one server, we'll have 8 + 4 = 12 units of money and we can upgrade the remaining 3 servers.

+ +

For the second data center, if we sell one server, we'll have 9 + 2 = 11 units of money and we can upgrade the remaining 2 servers.

+
+ +

Example 2:

+ +
+

Input: count = [1], upgrade = [2], sell = [1], money = [1]

+ +

Output: [0]

+
+ +

 

+

Constraints:

+ +
    +
  • 1 <= count.length == upgrade.length == sell.length == money.length <= 105
  • +
  • 1 <= count[i], upgrade[i], sell[i], money[i] <= 105
  • +
+ + + +## Solutions + + + +### Solution 1: Mathematics + +For each data center, we assume that we can upgrade $x$ servers, then $x \times \text{upgrade[i]} \leq \text{count[i]} \times \text{sell[i]} + \text{money[i]}$. That is, $x \leq \frac{\text{count[i]} \times \text{sell[i]} + \text{money[i]}}{\text{upgrade[i]} + \text{sell[i]}}$. Also, $x \leq \text{count[i]}$, so we can take the minimum of the two. + +The time complexity is $O(n)$, where $n$ is the length of the array. Ignoring the space consumption of the answer array, the space complexity is $O(1)$. + + + +#### Python3 + +```python +class Solution: + def maxUpgrades( + self, count: List[int], upgrade: List[int], sell: List[int], money: List[int] + ) -> List[int]: + ans = [] + for cnt, cost, income, cash in zip(count, upgrade, sell, money): + ans.append(min(cnt, (cnt * income + cash) // (cost + income))) + return ans +``` + +#### Java + +```java +class Solution { + public int[] maxUpgrades(int[] count, int[] upgrade, int[] sell, int[] money) { + int n = count.length; + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = Math.min(count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector maxUpgrades(vector& count, vector& upgrade, vector& sell, vector& money) { + int n = count.size(); + vector ans; + for (int i = 0; i < n; ++i) { + ans.push_back(min(count[i], (int) ((1LL * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])))); + } + return ans; + } +}; +``` + +#### Go + +```go +func maxUpgrades(count []int, upgrade []int, sell []int, money []int) (ans []int) { + for i, cnt := range count { + ans = append(ans, min(cnt, (cnt*sell[i]+money[i])/(upgrade[i]+sell[i]))) + } + return +} +``` + +#### TypeScript + +```ts +function maxUpgrades( + count: number[], + upgrade: number[], + sell: number[], + money: number[], +): number[] { + const n = count.length; + const ans: number[] = []; + for (let i = 0; i < n; ++i) { + const x = ((count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])) | 0; + ans.push(Math.min(x, count[i])); + } + return ans; +} +``` + + + + + + diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.cpp b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.cpp new file mode 100644 index 0000000000000..c8b90c097709e --- /dev/null +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + vector maxUpgrades(vector& count, vector& upgrade, vector& sell, vector& money) { + int n = count.size(); + vector ans; + for (int i = 0; i < n; ++i) { + ans.push_back(min(count[i], (int) ((1LL * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])))); + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.go b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.go new file mode 100644 index 0000000000000..e0753de1010f1 --- /dev/null +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.go @@ -0,0 +1,6 @@ +func maxUpgrades(count []int, upgrade []int, sell []int, money []int) (ans []int) { + for i, cnt := range count { + ans = append(ans, min(cnt, (cnt*sell[i]+money[i])/(upgrade[i]+sell[i]))) + } + return +} \ No newline at end of file diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java new file mode 100644 index 0000000000000..0d620e4ee1fec --- /dev/null +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java @@ -0,0 +1,10 @@ +class Solution { + public int[] maxUpgrades(int[] count, int[] upgrade, int[] sell, int[] money) { + int n = count.length; + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = Math.min(count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.py b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.py new file mode 100644 index 0000000000000..4a16031fc0406 --- /dev/null +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.py @@ -0,0 +1,8 @@ +class Solution: + def maxUpgrades( + self, count: List[int], upgrade: List[int], sell: List[int], money: List[int] + ) -> List[int]: + ans = [] + for cnt, cost, income, cash in zip(count, upgrade, sell, money): + ans.append(min(cnt, (cnt * income + cash) // (cost + income))) + return ans diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.ts b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.ts new file mode 100644 index 0000000000000..566f496fc1516 --- /dev/null +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.ts @@ -0,0 +1,14 @@ +function maxUpgrades( + count: number[], + upgrade: number[], + sell: number[], + money: number[], +): number[] { + const n = count.length; + const ans: number[] = []; + for (let i = 0; i < n; ++i) { + const x = ((count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])) | 0; + ans.push(Math.min(x, count[i])); + } + return ans; +} diff --git a/solution/README.md b/solution/README.md index 4d5022069bf07..86297bb930075 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3165,6 +3165,7 @@ | 3152 | [特殊数组 II](/solution/3100-3199/3152.Special%20Array%20II/README.md) | | 中等 | 第 398 场周赛 | | 3153 | [所有数对中数位不同之和](/solution/3100-3199/3153.Sum%20of%20Digit%20Differences%20of%20All%20Pairs/README.md) | | 中等 | 第 398 场周赛 | | 3154 | [到达第 K 级台阶的方案数](/solution/3100-3199/3154.Find%20Number%20of%20Ways%20to%20Reach%20the%20K-th%20Stair/README.md) | | 困难 | 第 398 场周赛 | +| 3155 | [Maximum Number of Upgradable Servers](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 5368441dc3d71..b9f7cf205c6e6 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3163,6 +3163,7 @@ Press Control + F(or Command + F on | 3152 | [Special Array II](/solution/3100-3199/3152.Special%20Array%20II/README_EN.md) | | Medium | Weekly Contest 398 | | 3153 | [Sum of Digit Differences of All Pairs](/solution/3100-3199/3153.Sum%20of%20Digit%20Differences%20of%20All%20Pairs/README_EN.md) | | Medium | Weekly Contest 398 | | 3154 | [Find Number of Ways to Reach the K-th Stair](/solution/3100-3199/3154.Find%20Number%20of%20Ways%20to%20Reach%20the%20K-th%20Stair/README_EN.md) | | Hard | Weekly Contest 398 | +| 3155 | [Maximum Number of Upgradable Servers](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md) | | Medium | 🔒 | ## Copyright From 882f92d83ad9da62c1780d067133d7d6c5622642 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 19 May 2024 20:55:03 +0800 Subject: [PATCH 2/2] fix: update --- .../3155.Maximum Number of Upgradable Servers/README.md | 3 ++- .../3155.Maximum Number of Upgradable Servers/README_EN.md | 3 ++- .../3155.Maximum Number of Upgradable Servers/Solution.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md index f6e1f6a029568..1109df6cae9a8 100644 --- a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README.md @@ -97,7 +97,8 @@ class Solution { int n = count.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { - ans[i] = Math.min(count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); + ans[i] = Math.min( + count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); } return ans; } diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md index 24d0c25ac9c32..8673c75d55792 100644 --- a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/README_EN.md @@ -97,7 +97,8 @@ class Solution { int n = count.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { - ans[i] = Math.min(count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); + ans[i] = Math.min( + count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); } return ans; } diff --git a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java index 0d620e4ee1fec..28650a7422372 100644 --- a/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java +++ b/solution/3100-3199/3155.Maximum Number of Upgradable Servers/Solution.java @@ -3,7 +3,8 @@ public int[] maxUpgrades(int[] count, int[] upgrade, int[] sell, int[] money) { int n = count.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { - ans[i] = Math.min(count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); + ans[i] = Math.min( + count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))); } return ans; }