LeetCode-in-Java

2861. Maximum Number of Alloys

Medium

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.

Example 1:

Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,0], cost = [1,2,3]

Output: 2

Explanation: It is optimal to use the 1st machine to create alloys.

To create 2 alloys we need to buy the:

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.

Example 2:

Input: n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,100], cost = [1,2,3]

Output: 5

Explanation: It is optimal to use the 2nd machine to create alloys. To create 5 alloys we need to buy:

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.

Example 3:

Input: n = 2, k = 3, budget = 10, composition = [[2,1],[1,2],[1,1]], stock = [1,1], cost = [5,5]

Output: 2

Explanation: It is optimal to use the 3rd machine to create alloys. To create 2 alloys we need to buy the:

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.

Constraints:

Solution

import java.util.List;

public class Solution {
    public int maxNumberOfAlloys(
            int n,
            int k,
            int budget,
            List<List<Integer>> composition,
            List<Integer> stock,
            List<Integer> cost) {
        int ans = 0;
        int max = 0;
        for (int i = 0; i < n; i++) {
            max = Math.max(stock.get(i), max);
        }
        for (int i = 0; i < k; i++) {
            int temp = 0;
            int low = 0;
            int high = max + budget;
            int mid;
            while (low <= high) {
                mid = low + (high - low) / 2;
                if (isPos(i, mid, n, budget, composition, stock, cost)) {
                    low = mid + 1;
                    temp = mid;
                } else {
                    high = mid - 1;
                }
            }
            ans = Math.max(ans, temp);
        }
        return ans;
    }

    private boolean isPos(
            int idx,
            int mid,
            int n,
            int budget,
            List<List<Integer>> composition,
            List<Integer> stock,
            List<Integer> cost) {
        long paiSa = 0L;
        for (int i = 0; i < n; i++) {
            long require = ((long) composition.get(idx).get(i)) * (mid);
            long have = stock.get(i);
            long diff = require - have;
            if (diff > 0) {
                paiSa += diff * ((long) cost.get(i));
            }
            if (budget < paiSa) {
                return false;
            }
        }
        return budget >= paiSa;
    }
}