Minimum Cost to Purchase Servers
Updated
Problem statement
AWS sells servers to its clients. One client wants to buy servers to run an application.
There are n servers. power[i] is the efficiency of server i: a whole number that measures its computing power. cost[i] is the number of AWS credits that server i costs. Every cost is either 1 or 2.
You want a set of servers whose total power is at least target. Each server can be bought only once. Return the smallest total cost of such a set. If all the servers together have less power than target, return -1.
For example, let power = [3, 5], cost = [1, 2] and target = 5. Server 0 alone has power 3, which is not enough. Server 1 alone has power 5 and costs 2. Buying both costs 3. The answer is 2.
Function
minCostToPurchaseServers(power: int[], cost: int[], target: int) → int
Complete the function minCostToPurchaseServers in the editor.
minCostToPurchaseServers has the following parameters:
int power[n]: the efficiency of each serverint cost[n]: the cost of each server, 1 or 2int target: the total power you need
Returns
int: the smallest total cost, or -1 if no set of servers is enough
Examples
Example 1
power = [4, 4, 6, 7]cost = [1, 1, 2, 2]target = 7return = 2A cost of 1 buys only one server with power 4, and 4 is less than 7. Servers 0 and 1 have power 4 + 4 = 8, which is at least 7. They cost 1 + 1 = 2. The answer is 2.
Constraints
1 <= n <= 10^5, wherenis the length ofpowerand ofcost1 <= power[i] <= 10^4cost[i]is 1 or 21 <= target <= 10^9
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Find Minimum TimeAlphaSense · OA
- Find Minimum TimeBlackRock · OABlackRock interview questions
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Minimum Cost of Every K-Capable Model SetTikTok · OATikTok interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
- Maximum Alternating-Index Packet ValueInfosys · OAInfosys interview questions
- Maximize Minimum Deployment DifficultyJP Morgan Chase · OAJP Morgan Chase interview questions