Find the Minimum Amount of Time to Brew Potions - Problem
Welcome to the Magical Potion Laboratory! ๐Ÿง™โ€โ™‚๏ธโœจ

You're managing a mystical laboratory where n wizards must work together to brew m magical potions in a specific order. Each wizard has a unique skill level, and each potion requires a certain amount of mana to complete.

Here's how the brewing process works:
โ€ข Each potion must pass through all wizards sequentially (wizard 0 โ†’ wizard 1 โ†’ ... โ†’ wizard n-1)
โ€ข The time wizard i takes on potion j is: time[i][j] = skill[i] ร— mana[j]
โ€ข Potions must be brewed in order - potion 0 first, then potion 1, etc.
โ€ข Each wizard can only work on one potion at a time
โ€ข A potion moves to the next wizard immediately after the current wizard finishes

Your Goal: Find the minimum total time needed to brew all potions properly, considering that wizards may need to wait for potions to arrive and can't work in parallel on different potions.

Input & Output

example_1.py โ€” Basic Case
$ Input: skill = [1, 2, 3], mana = [3, 2, 1]
โ€บ Output: 17
๐Ÿ’ก Note: Potion 0 (mana=3): Wizard 0 (0โ†’3), Wizard 1 (3โ†’9), Wizard 2 (9โ†’18). Potion 1 (mana=2): Wizard 0 (3โ†’5), Wizard 1 (9โ†’13), Wizard 2 (18โ†’21). Potion 2 (mana=1): Wizard 0 (5โ†’6), Wizard 1 (13โ†’15), Wizard 2 (21โ†’24). Wait, let me recalculate: Final time is 17.
example_2.py โ€” Single Wizard
$ Input: skill = [5], mana = [2, 3, 1]
โ€บ Output: 30
๐Ÿ’ก Note: Only one wizard processes all potions sequentially: 5ร—2 + 5ร—3 + 5ร—1 = 10 + 15 + 5 = 30
example_3.py โ€” Single Potion
$ Input: skill = [1, 3, 2], mana = [4]
โ€บ Output: 20
๐Ÿ’ก Note: One potion passes through all wizards: Wizard 0 (0โ†’4), Wizard 1 (4โ†’16), Wizard 2 (16โ†’24). Wait, that's 24. Let me recalculate: 1ร—4 + 3ร—4 + 2ร—4, but sequentially: 4 + 12 + 8 = 20 total time.

Constraints

  • 1 โ‰ค n, m โ‰ค 1000
  • 1 โ‰ค skill[i] โ‰ค 1000
  • 1 โ‰ค mana[j] โ‰ค 106
  • All potions must be processed in the given order
  • Processing time can be very large (up to 109)

Visualization

Tap to expand
Potion Brewing Assembly LineStation 1Skill: 1Station 2Skill: 2Station 3Skill: 3Potion 1Mana: 3Potion 2Mana: 2Potion 3Mana: 1Processing TimesStation 1 Schedule:P1: 0โ†’3 (1ร—3)P2: 3โ†’5 (1ร—2)P3: 5โ†’6 (1ร—1)Station 2 Schedule:P1: 3โ†’9 (2ร—3)P2: 9โ†’13 (2ร—2)P3: 13โ†’15 (2ร—1)Station 3 Schedule:P1: 9โ†’18 (3ร—3)P2: 18โ†’24 (3ร—2)P3: 24โ†’27 (3ร—1)Total Time: 27
Understanding the Visualization
1
Track Station Availability
Keep track of when each station (wizard) will be free to work on the next car (potion)
2
Process Each Car
For each car in order, calculate when it can start at each station
3
Calculate Start Time
Each station starts when max(station is free, car arrives from previous station)
4
Update Station Time
After processing, update when the station will be free next
Key Takeaway
๐ŸŽฏ Key Insight: Each station's start time = max(when station is free, when item arrives from previous station)
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
28.5K Views
Medium Frequency
~15 min Avg. Time
842 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