|
| 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> </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'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'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> </p> |
| 58 | +<p><strong>Constraints:</strong></p> |
| 59 | + |
| 60 | +<ul> |
| 61 | + <li><code>1 <= count.length == upgrade.length == sell.length == money.length <= 10<sup>5</sup></code></li> |
| 62 | + <li><code>1 <= count[i], upgrade[i], sell[i], money[i] <= 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 --> |
0 commit comments