#3155
Medium Algorithms Maximum number of upgradable servers
Array Math Binary Search
43.3% acceptance
Mar 31, 2026
20
2
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.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn max_upgrades(count: Vec<i32>, upgrade: Vec<i32>, sell: Vec<i32>, money: Vec<i32>) -> Vec<i32> {
let n = count.len();
let mut ans = vec![0i32; n];
for i in 0..n {
let c = count[i] as i64;
let u = upgrade[i] as i64;
let s = sell[i] as i64;
let m = money[i] as i64;
let k = ((m + c * s) / (u + s)).min(c);
ans[i] = k as i32;
}
ans
}
}