Skip to content

Commit 818daa9

Browse files
authored
feat: add solutions to lc problem: No.3155 (doocs#2843)
No.3155.Maximum Number of Upgradable Servers
1 parent ac3102d commit 818daa9

File tree

10 files changed

+372
-4
lines changed

10 files changed

+372
-4
lines changed

solution/3100-3199/3152.Special Array II/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3152.Sp
6161

6262
<!-- description:end -->
6363

64+
## Solutions
65+
66+
<!-- solution:start -->
67+
6468
### Solution 1: Record the Leftmost Special Array Position for Each Position
6569

6670
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.
6973

7074
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array.
7175

72-
<!-- solution:start -->
73-
74-
### Solution 1
75-
7676
<!-- tabs:start -->
7777

7878
#### Python3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3155. Maximum Number of Upgradable Servers 🔒](https://leetcode.cn/problems/maximum-number-of-upgradable-servers)
10+
11+
[English Version](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>You have <code>n</code> data centers and need to upgrade their servers.</p>
18+
19+
<p>You are given four arrays <code>count</code>, <code>upgrade</code>, <code>sell</code>, and <code>money</code> of length <code>n</code>, which show:</p>
20+
21+
<ul>
22+
<li>The number of servers</li>
23+
<li>The cost of upgrading a single server</li>
24+
<li>The money you get by selling a server</li>
25+
<li>The money you initially have</li>
26+
</ul>
27+
28+
<p>for each data center respectively.</p>
29+
30+
<p>Return an array <code>answer</code>, where for each data center, the corresponding element in <code>answer</code> represents the <strong>maximum</strong> number of servers that can be upgraded.</p>
31+
32+
<p>Note that the money from one data center <strong>cannot</strong> be used for another data center.</p>
33+
34+
<p>&nbsp;</p>
35+
<p><strong class="example">Example 1:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">count = [4,3], upgrade = [3,5], sell = [4,2], money = [8,9]</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">[3,2]</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<p>For the first data center, if we sell one server, we&#39;ll have <code>8 + 4 = 12</code> units of money and we can upgrade the remaining 3 servers.</p>
45+
46+
<p>For the second data center, if we sell one server, we&#39;ll have <code>9 + 2 = 11</code> units of money and we can upgrade the remaining 2 servers.</p>
47+
</div>
48+
49+
<p><strong class="example">Example 2:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>Input:</strong> <span class="example-io">count = [1], upgrade = [2], sell = [1], money = [1]</span></p>
53+
54+
<p><strong>Output:</strong> <span class="example-io">[0]</span></p>
55+
</div>
56+
57+
<p>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>1 &lt;= count.length == upgrade.length == sell.length == money.length &lt;= 10<sup>5</sup></code></li>
62+
<li><code>1 &lt;= count[i], upgrade[i], sell[i], money[i] &lt;= 10<sup>5</sup></code></li>
63+
</ul>
64+
65+
<!-- description:end -->
66+
67+
## 解法
68+
69+
<!-- solution:start -->
70+
71+
### 方法一:数学
72+
73+
对于每个数据中心,我们假设可以升级 $\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]}$,所以我们取两者的最小值即可。
74+
75+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
76+
77+
<!-- tabs:start -->
78+
79+
#### Python3
80+
81+
```python
82+
class Solution:
83+
def maxUpgrades(
84+
self, count: List[int], upgrade: List[int], sell: List[int], money: List[int]
85+
) -> List[int]:
86+
ans = []
87+
for cnt, cost, income, cash in zip(count, upgrade, sell, money):
88+
ans.append(min(cnt, (cnt * income + cash) // (cost + income)))
89+
return ans
90+
```
91+
92+
#### Java
93+
94+
```java
95+
class Solution {
96+
public int[] maxUpgrades(int[] count, int[] upgrade, int[] sell, int[] money) {
97+
int n = count.length;
98+
int[] ans = new int[n];
99+
for (int i = 0; i < n; ++i) {
100+
ans[i] = Math.min(
101+
count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])));
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
#### C++
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
vector<int> maxUpgrades(vector<int>& count, vector<int>& upgrade, vector<int>& sell, vector<int>& money) {
114+
int n = count.size();
115+
vector<int> ans;
116+
for (int i = 0; i < n; ++i) {
117+
ans.push_back(min(count[i], (int) ((1LL * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))));
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
124+
#### Go
125+
126+
```go
127+
func maxUpgrades(count []int, upgrade []int, sell []int, money []int) (ans []int) {
128+
for i, cnt := range count {
129+
ans = append(ans, min(cnt, (cnt*sell[i]+money[i])/(upgrade[i]+sell[i])))
130+
}
131+
return
132+
}
133+
```
134+
135+
#### TypeScript
136+
137+
```ts
138+
function maxUpgrades(
139+
count: number[],
140+
upgrade: number[],
141+
sell: number[],
142+
money: number[],
143+
): number[] {
144+
const n = count.length;
145+
const ans: number[] = [];
146+
for (let i = 0; i < n; ++i) {
147+
const x = ((count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])) | 0;
148+
ans.push(Math.min(x, count[i]));
149+
}
150+
return ans;
151+
}
152+
```
153+
154+
<!-- tabs:end -->
155+
156+
<!-- solution:end -->
157+
158+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3155. Maximum Number of Upgradable Servers 🔒](https://leetcode.com/problems/maximum-number-of-upgradable-servers)
10+
11+
[中文文档](/solution/3100-3199/3155.Maximum%20Number%20of%20Upgradable%20Servers/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>You have <code>n</code> data centers and need to upgrade their servers.</p>
18+
19+
<p>You are given four arrays <code>count</code>, <code>upgrade</code>, <code>sell</code>, and <code>money</code> of length <code>n</code>, which show:</p>
20+
21+
<ul>
22+
<li>The number of servers</li>
23+
<li>The cost of upgrading a single server</li>
24+
<li>The money you get by selling a server</li>
25+
<li>The money you initially have</li>
26+
</ul>
27+
28+
<p>for each data center respectively.</p>
29+
30+
<p>Return an array <code>answer</code>, where for each data center, the corresponding element in <code>answer</code> represents the <strong>maximum</strong> number of servers that can be upgraded.</p>
31+
32+
<p>Note that the money from one data center <strong>cannot</strong> be used for another data center.</p>
33+
34+
<p>&nbsp;</p>
35+
<p><strong class="example">Example 1:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>Input:</strong> <span class="example-io">count = [4,3], upgrade = [3,5], sell = [4,2], money = [8,9]</span></p>
39+
40+
<p><strong>Output:</strong> <span class="example-io">[3,2]</span></p>
41+
42+
<p><strong>Explanation:</strong></p>
43+
44+
<p>For the first data center, if we sell one server, we&#39;ll have <code>8 + 4 = 12</code> units of money and we can upgrade the remaining 3 servers.</p>
45+
46+
<p>For the second data center, if we sell one server, we&#39;ll have <code>9 + 2 = 11</code> units of money and we can upgrade the remaining 2 servers.</p>
47+
</div>
48+
49+
<p><strong class="example">Example 2:</strong></p>
50+
51+
<div class="example-block">
52+
<p><strong>Input:</strong> <span class="example-io">count = [1], upgrade = [2], sell = [1], money = [1]</span></p>
53+
54+
<p><strong>Output:</strong> <span class="example-io">[0]</span></p>
55+
</div>
56+
57+
<p>&nbsp;</p>
58+
<p><strong>Constraints:</strong></p>
59+
60+
<ul>
61+
<li><code>1 &lt;= count.length == upgrade.length == sell.length == money.length &lt;= 10<sup>5</sup></code></li>
62+
<li><code>1 &lt;= count[i], upgrade[i], sell[i], money[i] &lt;= 10<sup>5</sup></code></li>
63+
</ul>
64+
65+
<!-- description:end -->
66+
67+
## Solutions
68+
69+
<!-- solution:start -->
70+
71+
### Solution 1: Mathematics
72+
73+
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.
74+
75+
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)$.
76+
77+
<!-- tabs:start -->
78+
79+
#### Python3
80+
81+
```python
82+
class Solution:
83+
def maxUpgrades(
84+
self, count: List[int], upgrade: List[int], sell: List[int], money: List[int]
85+
) -> List[int]:
86+
ans = []
87+
for cnt, cost, income, cash in zip(count, upgrade, sell, money):
88+
ans.append(min(cnt, (cnt * income + cash) // (cost + income)))
89+
return ans
90+
```
91+
92+
#### Java
93+
94+
```java
95+
class Solution {
96+
public int[] maxUpgrades(int[] count, int[] upgrade, int[] sell, int[] money) {
97+
int n = count.length;
98+
int[] ans = new int[n];
99+
for (int i = 0; i < n; ++i) {
100+
ans[i] = Math.min(
101+
count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])));
102+
}
103+
return ans;
104+
}
105+
}
106+
```
107+
108+
#### C++
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
vector<int> maxUpgrades(vector<int>& count, vector<int>& upgrade, vector<int>& sell, vector<int>& money) {
114+
int n = count.size();
115+
vector<int> ans;
116+
for (int i = 0; i < n; ++i) {
117+
ans.push_back(min(count[i], (int) ((1LL * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))));
118+
}
119+
return ans;
120+
}
121+
};
122+
```
123+
124+
#### Go
125+
126+
```go
127+
func maxUpgrades(count []int, upgrade []int, sell []int, money []int) (ans []int) {
128+
for i, cnt := range count {
129+
ans = append(ans, min(cnt, (cnt*sell[i]+money[i])/(upgrade[i]+sell[i])))
130+
}
131+
return
132+
}
133+
```
134+
135+
#### TypeScript
136+
137+
```ts
138+
function maxUpgrades(
139+
count: number[],
140+
upgrade: number[],
141+
sell: number[],
142+
money: number[],
143+
): number[] {
144+
const n = count.length;
145+
const ans: number[] = [];
146+
for (let i = 0; i < n; ++i) {
147+
const x = ((count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])) | 0;
148+
ans.push(Math.min(x, count[i]));
149+
}
150+
return ans;
151+
}
152+
```
153+
154+
<!-- tabs:end -->
155+
156+
<!-- solution:end -->
157+
158+
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<int> maxUpgrades(vector<int>& count, vector<int>& upgrade, vector<int>& sell, vector<int>& money) {
4+
int n = count.size();
5+
vector<int> ans;
6+
for (int i = 0; i < n; ++i) {
7+
ans.push_back(min(count[i], (int) ((1LL * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i]))));
8+
}
9+
return ans;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func maxUpgrades(count []int, upgrade []int, sell []int, money []int) (ans []int) {
2+
for i, cnt := range count {
3+
ans = append(ans, min(cnt, (cnt*sell[i]+money[i])/(upgrade[i]+sell[i])))
4+
}
5+
return
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int[] maxUpgrades(int[] count, int[] upgrade, int[] sell, int[] money) {
3+
int n = count.length;
4+
int[] ans = new int[n];
5+
for (int i = 0; i < n; ++i) {
6+
ans[i] = Math.min(
7+
count[i], (int) ((1L * count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])));
8+
}
9+
return ans;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def maxUpgrades(
3+
self, count: List[int], upgrade: List[int], sell: List[int], money: List[int]
4+
) -> List[int]:
5+
ans = []
6+
for cnt, cost, income, cash in zip(count, upgrade, sell, money):
7+
ans.append(min(cnt, (cnt * income + cash) // (cost + income)))
8+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function maxUpgrades(
2+
count: number[],
3+
upgrade: number[],
4+
sell: number[],
5+
money: number[],
6+
): number[] {
7+
const n = count.length;
8+
const ans: number[] = [];
9+
for (let i = 0; i < n; ++i) {
10+
const x = ((count[i] * sell[i] + money[i]) / (upgrade[i] + sell[i])) | 0;
11+
ans.push(Math.min(x, count[i]));
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)