LeetCode 2861. 最大合金数 C#
原题链接
中等
作者:
hpstory
,
2023-09-21 17:42:00
,
所有人可见
,
阅读 87
C# 代码
public class Solution {
public int MaxNumberOfAlloys(int n, int k, int budget, IList<IList<int>> composition, IList<int> stock, IList<int> cost) {
int result = 0;
for (int i = 0; i < k; i++){
result = Math.Max(result, Check(composition[i]));
}
return result;
int Check(IList<int> x){
long left = 0, right = 1000000000;
while (left < right){
long mid = left + right + 1 >> 1;
long money = 0;
for (int j = 0; j < n; j++){
long t = x[j] * mid;
if (stock[j] <= t){
money += (t - stock[j]) * cost[j];
}
}
if (budget - money >= 0){
left = mid;
}
else{
right = mid - 1;
}
}
return (int)left;
}
}
}