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 ith 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 1-indexed 2D array composition, and
1-indexed 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: n =3, k =2, budget =15, composition =[[1,1,1],[1,1,10]], stock =[0,0,0], cost =[1,2,3]Output: 2Explanation: It is optimal to use the 1st machine to create alloys.To create 2 alloys we need to buy the:-2 units of metal of the 1st type.-2 units of metal of the 2nd type.-2 units of metal of the 3rd type.In total, we need 2*1+2*2+2*3=12 coins, which is smaller than or equal to budget =15.Notice that we have 0 units of metal of each type and we have to buy all the required units of metal.It can be proven that we can create at most 2 alloys.
Input: n =3, k =2, budget =15, composition =[[1,1,1],[1,1,10]], stock =[0,0,100], cost =[1,2,3]Output: 5Explanation: It is optimal to use the 2nd machine to create alloys.To create 5 alloys we need to buy:-5 units of metal of the 1st type.-5 units of metal of the 2nd type.-0 units of metal of the 3rd type.In total, we need 5*1+5*2+0*3=15 coins, which is smaller than or equal to budget =15.It can be proven that we can create at most 5 alloys.
Input: n =2, k =3, budget =10, composition =[[2,1],[1,2],[1,1]], stock =[1,1], cost =[5,5]Output: 2Explanation: It is optimal to use the 3rd machine to create alloys.To create 2 alloys we need to buy the:-1 unit of metal of the 1st type.-1 unit of metal of the 2nd type.In total, we need 1*5+1*5=10 coins, which is smaller than or equal to budget =10.It can be proven that we can create at most 2 alloys.
We want to maximize the number of alloys we can make using one machine, given limited stock and a budget for buying more metals. For any fixed number of alloys, we can check if it’s possible to make that many alloys with any machine within the budget. We use binary search to find the maximum possible number.
classSolution {
public:int maxNumberOfAlloys(int n, int k, int budget, vector<vector<int>>& composition, vector<int>& stock, vector<int>& cost) {
int l =0, r =1e8, ans =0;
while (l <= r) {
int mid = l + (r - l) /2;
bool ok = false;
for (int i =0; i < k; ++i) {
longlong total =0;
for (int j =0; j < n; ++j) {
longlong need =1LL* composition[i][j] * mid - stock[j];
if (need >0) total += need * cost[j];
}
if (total <= budget) ok = true;
}
if (ok) {
ans = mid;
l = mid +1;
} else {
r = mid -1;
}
}
return ans;
}
};
classSolution {
publicintmaxNumberOfAlloys(int n, int k, int budget, int[][] composition, int[] stock, int[] cost) {
int l = 0, r = (int)1e8, ans = 0;
while (l <= r) {
int mid = l + (r - l) / 2;
boolean ok =false;
for (int i = 0; i < k; i++) {
long total = 0;
for (int j = 0; j < n; j++) {
long need = 1L * composition[i][j]* mid - stock[j];
if (need > 0) total += need * cost[j];
}
if (total <= budget) ok =true;
}
if (ok) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
}
classSolution {
funmaxNumberOfAlloys(n: Int, k: Int, budget: Int, composition: Array<IntArray>, stock: IntArray, cost: IntArray): Int {
var l = 0var r = 1e8.toInt()
var ans = 0while (l <= r) {
val mid = (l + r) / 2var ok = falsefor (i in0 until k) {
var total = 0Lfor (j in0 until n) {
val need = 1L * composition[i][j] * mid - stock[j]
if (need > 0) total += need * cost[j]
}
if (total <= budget) ok = true }
if (ok) {
ans = mid
l = mid + 1 } else {
r = mid - 1 }
}
return ans
}
}
classSolution:
defmaxNumberOfAlloys(self, n: int, k: int, budget: int, composition: list[list[int]], stock: list[int], cost: list[int]) -> int:
l, r, ans =0, 10**8, 0while l <= r:
mid = (l + r) //2 ok =Falsefor i in range(k):
total =0for j in range(n):
need = composition[i][j] * mid - stock[j]
if need >0:
total += need * cost[j]
if total <= budget:
ok =Trueif ok:
ans = mid
l = mid +1else:
r = mid -1return ans
impl Solution {
pubfnmax_number_of_alloys(n: i32, k: i32, budget: i32, composition: Vec<Vec<i32>>, stock: Vec<i32>, cost: Vec<i32>) -> i32 {
let (n, k) = (n asusize, k asusize);
letmut l =0;
letmut r =100_000_000;
letmut ans =0;
while l <= r {
let mid = (l + r) /2;
letmut ok =false;
for i in0..k {
letmut total =0i64;
for j in0..n {
let need = composition[i][j] asi64* mid asi64- stock[j] asi64;
if need >0 {
total += need * cost[j] asi64;
}
}
if total <= budget asi64 {
ok =true;
}
}
if ok {
ans = mid;
l = mid +1;
} else {
if mid ==0 { break; }
r = mid -1;
}
}
ans
}
}