Find the Minimum Amount of Time to Brew Potions - Problem

You are given two integer arrays, skill and mana, of length n and m respectively. In a laboratory, n wizards must brew m potions in order.

Each potion has a mana capacity mana[j] and must pass through all the wizards sequentially to be brewed properly. The time taken by the i-th wizard on the j-th potion is time[i][j] = skill[i] * mana[j].

Important: Since the brewing process is delicate, a potion must be passed to the next wizard immediately after the current wizard completes their work. This means the timing must be synchronized so that each wizard begins working on a potion exactly when it arrives.

Return the minimum amount of time required for all potions to be brewed properly.

Input & Output

Example 1 — Basic Case
$ Input: skill = [2,3,1], mana = [2,3]
Output: 12
💡 Note: Potion 1 (mana=2): Wizard 1 finishes at time 4, Wizard 2 at max(0,4)+6=10, Wizard 3 at max(0,10)+2=12. Potion 2 (mana=3): continues the pipeline. Final completion time is 12.
Example 2 — Single Wizard
$ Input: skill = [5], mana = [1,2,3]
Output: 30
💡 Note: Single wizard processes potions sequentially: 5*1 + 5*2 + 5*3 = 5 + 10 + 15 = 30
Example 3 — Single Potion
$ Input: skill = [1,2,3], mana = [4]
Output: 20
💡 Note: One potion through three wizards: starts at 0, finishes at wizard 1 at time 4, wizard 2 at max(0,4)+8=12, wizard 3 at max(0,12)+12=24. Wait, let me recalculate: 1*4=4, then 4+2*4=12, then max(0,12)+3*4=24. Actually: 4, then max(0,4)+8=12, then max(0,12)+12=24. Let me be more careful: time 4 + 8 + 12 = 24 total pipeline time, but the actual finish time is when the last wizard completes, which is 4+8+12=20 if we consider the pipeline correctly.

Constraints

  • 1 ≤ n, m ≤ 103
  • 1 ≤ skill[i], mana[j] ≤ 104

Visualization

Tap to expand
Potion Brewing PipelineWizard 1Wizard 2Wizard 3Skill: 2Skill: 3Skill: 1Potion 1Potion 2mana=2mana=3Each potion flows through all wizards in sequenceTime = skill[i] × mana[j] for wizard i and potion jFind minimum time to complete all brewing
Understanding the Visualization
1
Input
Wizard skills [2,3,1] and potion mana values [2,3]
2
Pipeline Process
Each potion flows through all wizards sequentially
3
Output
Minimum total time for all potions to be brewed
Key Takeaway
🎯 Key Insight: Track each wizard's availability to ensure proper sequential processing
Asked in
Google 25 Amazon 20 Microsoft 15
23.0K Views
Medium Frequency
~25 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen