Skip to content

2861. Maximum Number of Alloys 👍

  • Time: $O(\log 10^9 \cdot kn)$
  • Space: $O(1)$
 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
28
29
30
31
32
33
34
35
36
37
class Solution {
 public:
  int maxNumberOfAlloys(int n, int k, int budget,
                        vector<vector<int>>& composition, vector<int>& stock,
                        vector<int>& cost) {
    int l = 1;
    int r = 1'000'000'000;

    while (l < r) {
      const int m = (l + r) / 2;
      if (isPossible(n, budget, composition, stock, cost, m))
        l = m + 1;
      else
        r = m;
    }

    return l - 1;
  }

 private:
  // Returns true if it's possible to create `m` alloys by using any machine.
  bool isPossible(int n, int budget, const vector<vector<int>>& composition,
                  const vector<int>& stock, const vector<int>& costs, int m) {
    // Try all the possible machines.
    for (const vector<int>& machine : composition) {
      long requiredMoney = 0;
      for (int j = 0; j < n; ++j) {
        const long requiredUnits =
            max(0L, static_cast<long>(machine[j]) * m - stock[j]);
        requiredMoney += static_cast<long>(requiredUnits) * costs[j];
      }
      if (requiredMoney <= budget)
        return true;
    }
    return false;
  }
};
 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
28
29
30
31
32
33
class Solution {
  public int maxNumberOfAlloys(int n, int k, int budget, List<List<Integer>> composition,
                               List<Integer> stock, List<Integer> cost) {
    int l = 1;
    int r = 1_000_000_000;

    while (l < r) {
      final int m = (l + r) / 2;
      if (isPossible(n, budget, composition, stock, cost, m))
        l = m + 1;
      else
        r = m;
    }

    return l - 1;
  }

  // Returns true if it's possible to create `m` alloys by using any machine.
  private boolean isPossible(int n, int budget, List<List<Integer>> composition,
                             List<Integer> stock, List<Integer> costs, int m) {
    // Try all the possible machines.
    for (List<Integer> machine : composition) {
      long requiredMoney = 0;
      for (int j = 0; j < n; ++j) {
        final long requiredUnits = Math.max(0L, (long) machine.get(j) * m - stock.get(j));
        requiredMoney += requiredUnits * costs.get(j);
      }
      if (requiredMoney <= budget)
        return true;
    }
    return false;
  }
}
 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:
  def maxNumberOfAlloys(self, n: int, k: int, budget: int,
                        composition: list[list[int]], stock: list[int],
                        costs: list[int]) -> int:
    l = 1
    r = 1_000_000_000

    def isPossible(m: int) -> bool:
      """Returns True if it's possible to create `m` alloys by using any machine."""
      # Try all the possible machines.
      for machine in composition:
        requiredMoney = 0
        for j in range(n):
          requiredUnits = max(0, machine[j] * m - stock[j])
          requiredMoney += requiredUnits * costs[j]
        if requiredMoney <= budget:
          return True
      return False

    while l < r:
      m = (l + r) // 2
      if isPossible(m):
        l = m + 1
      else:
        r = m

    return l - 1