Ctrl K

Batches From One Line

Medium
A factory has lines assembly lines that all build the same product from parts of kinds different kinds. Their recipes differ, though: line i needs recipes[i][j] parts of kind j for one product. The warehouse holds stock[j] parts of kind j. Missing parts can be bought: one part of kind j costs prices[j] coins, and you may spend at most budget coins in total. All products must be built on the same line, which you choose. Return the largest number of products you can build.
Example 1
kinds=2, lines=2, budget=12, recipes=[[2, 1], [1, 3]], stock=[4, 0], prices=[3, 2] → 3

On line 0, three batches need 6 and 3 parts: buy 2 parts of kind 0 and 3 of kind 1 for 2·3 + 3·2 = 12. Four batches would cost 4·3 + 4·2 = 20. Line 1 manages only 2 batches (6 parts of kind 1 for 12 coins). The answer is 3.

Example 2
kinds=1, lines=1, budget=0, recipes=[[5]], stock=[12], prices=[7] → 2

There is no money, and the stock covers 12 // 5 = 2 batches.

Constraints
  • 1 ≤ kinds, lines ≤ 100
  • 0 ≤ budget ≤ 10⁸
  • len(recipes) = lines, len(recipes[i]) = kinds
  • 1 ≤ recipes[i][j] ≤ 100
  • len(stock) = len(prices) = kinds
  • 0 ≤ stock[j] ≤ 10⁸
  • 1 ≤ prices[j] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFind in a Shifted Sorted Array MediumFirst and Last Copy MediumFind in a Row-Chained Grid MediumFind in a Shifted Array With Repeats MediumMinimum of a Shifted List Medium
loading Python…
Input
kinds=2, lines=2, budget=12, recipes=[[2, 1], [1, 3]], stock=[4, 0], prices=[3, 2]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 84 tests on the server, hidden ones included.