Maximum Number of Alloys - Problem

You are the owner of a company that creates alloys using various types of metals. There are n different types of metals available, and you have access to k machines that can be used to create alloys.

Each machine requires a specific amount of each metal type to create an alloy. For the i-th machine to create an alloy, it needs composition[i][j] units of metal of type j.

Initially, you have stock[i] units of metal type i, and purchasing one unit of metal type i costs cost[i] coins.

Given integers n, k, budget, a 2D array composition, and arrays stock and cost, your goal is to maximize the number of alloys the company can create while staying within the budget of budget coins.

All alloys must be created with the same machine.

Return the maximum number of alloys that the company can create.

Input & Output

Example 1 — Basic Manufacturing
$ Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,2]], stock = [0,0,0], cost = [1,2,3]
Output: 2
💡 Note: Machine 0 needs [1,1,1] per alloy: cost = 1×1 + 1×2 + 1×3 = 6 per alloy. With budget 15, we can make 15÷6 = 2 alloys. Machine 1 needs [1,1,2] per alloy: cost = 1×1 + 1×2 + 2×3 = 9 per alloy. With budget 15, we can make 15÷9 = 1 alloy. Maximum is 2.
Example 2 — With Initial Stock
$ Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,2]], stock = [1,1,0], cost = [1,2,3]
Output: 5
💡 Note: Machine 0: For 5 alloys need [5,5,5]. Have [1,1,0], need to buy [4,4,5]. Cost = 4×1 + 4×2 + 5×3 = 27 > 15. For 4 alloys need [4,4,4], buy [3,3,4]. Cost = 3×1 + 3×2 + 4×3 = 21 > 15. For 3 alloys need [3,3,3], buy [2,2,3]. Cost = 2×1 + 2×2 + 3×3 = 15 ≤ 15. So machine 0 can make 3. Machine 1 can make fewer, so answer is 5 (need to verify calculation).
Example 3 — Single Machine
$ Input: n = 2, k = 1, budget = 10, composition = [[2,1]], stock = [1,1], cost = [1,1]
Output: 5
💡 Note: Only machine 0: needs [2,1] per alloy. For 5 alloys need [10,5]. Have [1,1], need to buy [9,4]. Cost = 9×1 + 4×1 = 13 > 10. For 4 alloys need [8,4], buy [7,3]. Cost = 7×1 + 3×1 = 10 ≤ 10. So maximum is 4 alloys.

Constraints

  • 1 ≤ n, k ≤ 100
  • 0 ≤ budget ≤ 108
  • composition[i][j] ≥ 1
  • 0 ≤ stock[i] ≤ 108
  • 1 ≤ cost[i] ≤ 100

Visualization

Tap to expand
Maximum Number of Alloys: Choose Best MachineMachine 0[1,1,1]Cost: 6/alloyMachine 1[1,1,2]Cost: 9/alloyMachine 2[1,1,3]Cost: 12/alloyBudget 15 → 2 alloysBudget 15 → 1 alloyBudget 15 → 1 alloyStock & Costsstock = [0,0,0]cost = [1,2,3]Choose Machine 0!Maximum: 2 Alloys
Understanding the Visualization
1
Input
Machines with different metal requirements and budget constraints
2
Process
For each machine, find maximum affordable alloys using binary search
3
Output
Return the highest number achievable from any single machine
Key Takeaway
🎯 Key Insight: Binary search on the answer space is perfect since cost increases monotonically with alloy count
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
18.5K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen