Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
22
Indexable
Never
def maxUpgradedServers(num_servers, money, upgrade, sell):
    n = len(num_servers)
    max_upgraded_servers = [0] * n

    # Create a list of tuples (cost, network index) to keep track of the cost for each network.
    network_costs = [(upgrade[i], i) for i in range(n)]

    # Sort the network_costs list in ascending order of upgrade cost.
    network_costs.sort()

    for cost, network_index in network_costs:
        funds = money[network_index]

        # Calculate the maximum number of servers that can be upgraded for this network.
        max_servers_upgraded = min(funds // cost, num_servers[network_index])

        # Update the funds and max_upgraded_servers list.
        funds -= max_servers_upgraded * cost
        max_upgraded_servers[network_index] = max_servers_upgraded

        # If there is any remaining budget, consider selling servers.
        if funds > 0:
            servers_sold = min(funds // sell[network_index], num_servers[network_index])
            max_upgraded_servers[network_index] += servers_sold

    return max_upgraded_servers

# Example usage:
num_servers = [4,3]
money = [8,9]
upgrade = [4,2]
sell = [4,5]

result = maxUpgradedServers(num_servers, money, upgrade, sell)
print(result)