FastPrepFastPrep
Problem Brief

Minimum Cost to Purchase Servers

FULLTIMEOA
See Amazon online assessment and hiring insights

SDE II

Another purchaing servers problem for AMZ full-time - Find Max Num Of Servers

AWS delivers various computing machines tailored to their customers' deployment and processing requirements. One particular AWS user is looking to acquire some machines for hosting their service.

You are provided with details of n machines represented through two lists: power and price. Here, power is a theoretical integer scale indicating the machine’s computing strength, while price specifies the number of AWS credits needed to acquire the machine. For simplicity, each machine has a price value of either 1 or 2. Your task is to determine the least possible overall price to obtain a collection of machines such that the cumulative power is at least a given threshold target.

If no selection of machines can achieve a total power that meets or surpasses target, return -1.

Note: A machine can only be chosen once.

Function Description

Complete the function minCostToPurchaseServers in the editor.

minCostToPurchaseServers has the following parameters:

  1. 1. int[] power: a list of integers denoting the computational strength of the machines
  2. 2. int[] price: a list of integers indicating the cost of the machines
  3. 3. int target: the minimum required total computational strength

Returns

int: the least total cost to obtain machines, or -1 if it’s unachievable

1Example 1

Input
power = [4, 4, 6, 7], cost = [1, 1, 2, 2], target = 7
Output
2
Explanation
If we go with machines at positions [0, 1], the total price is 1 + 1 = 2, which is the lowest, and the combined power is 4 + 4 = 8, which meets the requirement of being at least target. Selecting machines [0, 2] results in a total price of 1 + 2 = 3 and total power of 4 + 6 = 10 Choosing [1, 2] gives the same outcome: price = 3, power = 10. Other combinations can be considered similarly. Among all valid choices, the selection [0, 1] yields the smallest total price (which is 2) while ensuring the accumulated power surpasses the given threshold target = 7.
public int minCostToPurchaseServers(int[] power, int[] cost, int target) {
  // write your code here
}
Input

power

[4, 4, 6, 7]

cost

[1, 1, 2, 2]

target

7

Output

2

Sign in to submit your solution.