Ctrl K

Server Upgrades per Site

Medium
A company runs n server sites. For site i you know four numbers: - servers[i] — how many servers it has; - upgrade_cost[i] — the cost of upgrading one server; - resale[i] — how much money selling one server brings in; - budget[i] — how much money the site starts with. At each site you may sell some servers and spend the proceeds plus the budget on upgrading some of the servers that remain. A sold server can no longer be upgraded. Money from one site cannot be spent at another. Return an array giving, for each site, the largest number of servers that can be upgraded there.
Example 1
servers=[5, 2], upgrade_cost=[4, 10], resale=[2, 3], budget=[10, 1] → [3, 0]

At the first site, sell one server: 10 + 2 = 12, enough to upgrade three of the remaining four (3·4 = 12). At the second site, even selling one server leaves only 1 + 3 = 4 < 10, so the answer is 0.

Example 2
servers=[3], upgrade_cost=[2], resale=[1], budget=[6] → [3]

There is enough money to upgrade all three servers without selling any.

Constraints
  • 1 ≤ n ≤ 10⁵, all four arrays have length n
  • 1 ≤ servers[i], upgrade_cost[i], resale[i] ≤ 10⁵
  • 1 ≤ budget[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrices That Fade With Each Sale MediumEver-Growing Teams MediumShave Primes Into Order MediumLevel Out to a Palindrome MediumA Crew Levels a Hill Medium
loading Python…
Input
servers=[5, 2], upgrade_cost=[4, 10], resale=[2, 3], budget=[10, 1]
Expected
[3, 0]

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.