Link: https://leetcode.com/problems/maximum-number-of-alloys
Solution:
DSA: binary search
Intuition
The key to this problem is understanding that if we can make X alloys with a machine, then we can also by definition make X-1 alloys with that machine. Since the number of alloys made has this monotonic property, we can perform binary search on a range of 1 alloy to max alloys for each machine to get our result.
The upper bound of alloys is a bit tricky, but logically it cannot be more than sum(stock)+budget
if we assume the best case of a composition of all 1’s.
It may be tempting to simulate each alloy being created iteratively, but this results in TLE and with some simple math, we can compute if a machine can create X many alloys in O(len(compostion[i]))
time instead of O(qty)
time. The key is to compute the total number of each metal needed is to determine the number of each metal needed (metal * qty) and subtract the stock. Then if the number of metals needed is greater than zero, we multiply that by the respective cost and add that to total cost. If the total cost is smaller than the budget, we can create that quantity. If we can create that quantity with even a single machine, we then move our binary search towards the upper bound.
Implementation
Visual
Review 1
Nice binary search problem. Just perform a binary search for each machine on the number of alloys that can be created while staying under budget. Take the max of all.