Problem · Greedy

Server Investment

Learn this problem
EasySnowflakeINTERNOA
See Snowflake hiring insights

Problem statement

A network security administrator must protect networks at a number of locations from cyber-attacks. Initially, the ith network has num_servers[i] servers, and money[i] funds allocated for security upgrades. To upgrade a server in the ith network, it costs upgrade[i]. Selling a server adds sell[i] to available funds.

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

getMaxUpgradedServers(num_servers: int[], money: int[], sell: int[], upgrade: int[]) → int[]

Complete the function getMaxUpgradedServers in the editor.

getMaxUpgradedServers has the following parameter(s):

  1. int num_servers[n]: the number of servers in each network
  2. int money[n]: the initial amount of money at each network location
  3. int sell[n]: the network-specific value of selling a server
  4. int upgrade[n]: the network-specific cost to upgrade a server

Returns

int[]: The maximum number of upgraded servers for each network

Examples

Example 1

num_servers = [4, 3]money = [8, 9]sell = [4, 2]upgrade = [4, 5]return = [3, 2]
Example 1 illustration
Explanation is shown in above image 👆. Hence, answer is [3, 2]~

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ num_servers[i], money[i], sell[i], upgrade[i] ≤ 104
  • More Snowflake problems

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