leetcode
leetcode 2451 ~ 2500
最大合金数

最大合金数

难度:

标签:

题目描述

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:
- 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.

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:
- 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.

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:
- 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.

 

Constraints:

  • 1 <= n, k <= 100
  • 0 <= budget <= 108
  • composition.length == k
  • composition[i].length == n
  • 1 <= composition[i][j] <= 100
  • stock.length == cost.length == n
  • 0 <= stock[i] <= 108
  • 1 <= cost[i] <= 100

代码结果

运行时间: 66 ms, 内存: 16.5 MB


/*
 * 思路:
 * 1. 使用Java Stream API计算每台机器在预算内能制造的最大合金数量。
 * 2. 对于每一台机器,计算需要的成本并在预算范围内调整制造数量。
 * 3. 返回所有机器中能制造的最大合金数量。
 */
import java.util.stream.IntStream;

public class AlloyManufacturingStream {
    public int maxAlloys(int n, int k, int budget, int[][] composition, int[] stock, int[] cost) {
        return IntStream.range(0, k)
                .map(i -> calculateMaxAlloys(composition[i], stock, cost, budget))
                .max()
                .orElse(0);
    }

    private int calculateMaxAlloys(int[] comp, int[] stock, int[] cost, int budget) {
        int low = 0, high = budget + 1;
        while (low < high) {
            int mid = low + (high - low) / 2;
            if (canMake(comp, stock, cost, mid, budget)) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low - 1;
    }

    private boolean canMake(int[] comp, int[] stock, int[] cost, int numAlloys, int budget) {
        int totalCost = 0;
        for (int i = 0; i < comp.length; i++) {
            int required = comp[i] * numAlloys;
            if (required > stock[i]) {
                totalCost += (required - stock[i]) * cost[i];
            }
            if (totalCost > budget) return false;
        }
        return totalCost <= budget;
    }
}

解释

方法:

这个题解使用了一个贪心的方法来为每台机器计算最大可能制造的合金数量。对于每一台机器,它计算了在当前预算下每种金属可以制造的合金数量的上限,并按照这个上限升序排序。这样,每次需要额外购买最少的金属数量来制造更多的合金。分析完毕后,它计算了在当前预算下可以制造的最大合金数量。最后,通过对所有机器重复这一过程,找到并返回可以制造最多合金的机器的结果。

时间复杂度:

O(k * n log n)

空间复杂度:

O(n)

代码细节讲解

🦆
在题解中,为什么选择使用贪心算法而不是动态规划或其他算法来解决这个问题?
贪心算法在这个问题中被选用是因为它提供了一种直观而高效的方法来逐步优化决策,即优先购买单位成本下能增加更多合金数量的金属。这种方法通常比动态规划更简单且执行效率更高,特别是在问题规模较大时。动态规划虽然可以处理更复杂的依赖情况,但在此问题中可能导致不必要的计算复杂性和更高的时间及空间成本。
🦆
在计算每种金属可以制造的合金数量时,排序的依据是什么?为什么要按照'可以制造的合金数'的升序进行排序?
排序的依据是每种金属使用当前库存能制造的合金数量,即库存量除以每单位合金所需的该金属量。按照'可以制造的合金数'的升序进行排序的目的是优先处理那些制造合金数量较少的金属。这样做是因为处理这些金属可能需要较少的预算增量,从而在预算有限的情况下,尽可能多地制造合金。
🦆
题解中提到'如果增加当前金属超出预算,中断循环',请问这种做法是否可能导致未找到实际的最大合金数?
这种做法基于贪心算法的局限性,可能会导致未找到实际的最大合金数。因为这种方法是依据当前排序和预算决定是否继续购买,而不是全面考虑所有可能的购买组合。这可能导致某些情况下,通过不同的购买策略(可能初期成本较高但长远更经济)能够制造更多的合金。
🦆
在计算最大合金数的公式`(stock_cost + budget) // alloy_cost`中,`stock_cost`和`alloy_cost`的具体计算方式是什么?
`stock_cost`是指使用现有库存的金属的总成本,计算方式是每种金属的库存量乘以该金属的单价的总和。`alloy_cost`是指在购买额外金属后,每生产一个单位合金的平均成本,这是通过将每种金属的单价与每单位合金所需的金属量相乘并累加得到的。公式中的`(stock_cost + budget) // alloy_cost`表示在有限预算内最多可以制造的合金数量。

相关问题