Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2861: Maximum Number of Alloys

In this guide, we solve Leetcode #2861 Maximum Number of Alloys in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

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.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Array, Binary Search

Intuition

The problem structure suggests a monotonic decision, which makes binary search a natural fit.

By halving the search space each step, we reach the answer efficiently.

Approach

Search either directly on a sorted array or on the answer space using a check function.

Each check is fast, and the logarithmic search keeps the overall runtime low.

Steps:

  • Define the search bounds.
  • Check the mid point condition.
  • Narrow the bounds until convergence.

Example

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.

Python Solution

class Solution: def maxNumberOfAlloys( self, n: int, k: int, budget: int, composition: List[List[int]], stock: List[int], cost: List[int], ) -> int: ans = 0 for c in composition: l, r = 0, budget + stock[0] while l < r: mid = (l + r + 1) >> 1 s = sum(max(0, mid * x - y) * z for x, y, z in zip(c, stock, cost)) if s <= budget: l = mid else: r = mid - 1 ans = max(ans, l) return ans

Complexity

The time complexity is O(n×k×log⁡M)O(n \times k \times \log M)O(n×k×logM), where MMM is the upper bound of the binary search, and in this problem, M≤2×108M \leq 2 \times 10^8M≤2×108. The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy