Problem Β· Array

Server Investment 🐳

Learn this problem
● MediumTiktokOA
See Tiktok hiring insights

Problem statement

A network security administrator must protect networks at several locations from cyber-attacks.

Initially, the nth network has num_servers[i] servers, and money[i] funds allocated for security upgrades. To upgrade a server in the n^{th} network, it costs upgrade[i]. Selling a server adds sell[i] to available funds.

Formally, Given the arrays num_servers, money, upgrade, and sell, each with n integers, determine the maximum number of servers that can be upgraded to ensure optimal network security.

The result should be an array of n integers, where the ith integer represents the maximum number of upgraded servers for the ith network system.

Function

maximizeUpgradedServers(num_servers: int[], money: int[], upgrade: int[], sell: int[]) β†’ int[]

Examples

Example 1

num_servers = [4, 3]money = [8, 9]upgrade = [4, 5]sell = [4, 2]return = [3, 2]
Example 1 illustration
For the first network, sell one server to increase the funds from 8 to 12. The remaining three servers can then all be upgraded for 3 Γ— 4 = 12. For the second network, sell one server to increase the funds from 9 to 11. The remaining two servers can then both be upgraded for 2 Γ— 5 = 10. Therefore, return [3, 2].

Constraints

  • All four input arrays have the same non-zero length n.
  • num_servers[i] >= 0, money[i] >= 0, sell[i] >= 0, and upgrade[i] > 0.
  • For each network, any whole number of servers from 0 through num_servers[i] may be sold before upgrading the remaining servers.
  • The result for network i is between 0 and num_servers[i], inclusive.

More Tiktok problems

drafts saved locally
public int[] maximizeUpgradedServers(int[] num_servers, int[] money, int[] upgrade, int[] sell) {
  // write your code here
}
num_servers[4, 3]
money[8, 9]
upgrade[4, 5]
sell[4, 2]
expected[3, 2]
checking account