1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class Solution { public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition, List<Integer> stock, List<Integer> cost) { long res = 0; for (List<Integer> comp : composition) { long l = 0, r = Integer.MAX_VALUE; while (l <= r) { long mid = (l + r) >> 1; if (check(mid, n, budget, comp, stock, cost)) l = mid + 1; else r = mid - 1; } res = Math.max(res, r); } return (int) res; }
private boolean check(long cnt, int n, long budget, List<Integer> comp, List<Integer> stock,List<Integer> cost) { long[] requires = new long[n]; for (int i = 0; i < n; ++i) { requires[i] = Math.max(comp.get(i) * cnt - stock.get(i), 0); budget -= requires[i] * cost.get(i); } return budget >= 0; }
}
|