mediumQueuePattern: Design

Sequential Brewing Schedule Solution

Problem Statement

Given a list of potion brewing tasks, each with a brewing time and a sequence of ingredient addition times, implement a queue to manage the brewing process and determine the order in which the potions should be brewed.

Examples

Example 1:
Input:[{ 'brewing_time': 10, 'ingredients': [2, 5, 8] }, { 'brewing_time': 8, 'ingredients': [1, 4, 7] }]
Output:["[{","'brewing_time':","10,","'ingredients':","[2,","5,","8]","},","{","'brewing_time':","8,","'ingredients':","[1,","4,","7]","}]"]
Explanation: The brewing tasks are ordered based on their brewing times and ingredient addition times.
Example 2:
Input:[{ 'brewing_time': 5, 'ingredients': [1] }, { 'brewing_time': 5, 'ingredients': [2] }]
Output:["[{","'brewing_time':","5,","'ingredients':","[1]","},","{","'brewing_time':","5,","'ingredients':","[2]","}]"]
Explanation: When multiple brewing tasks have the same brewing time, their order is determined by the order of their ingredient addition times.
Example 3:
Input:[{ 'brewing_time': 15, 'ingredients': [3, 6, 9, 12] }]
Output:["[{","'brewing_time':","15,","'ingredients':","[3,","12]","6,","9,","}]"]
Explanation: A single brewing task is ordered based on its brewing time and ingredient addition times.

Constraints

  • 1 <= number of brewing tasks <= 1000
  • 1 <= brewing time <= 1000
  • 1 <= number of ingredients <= 10
  • 1 <= ingredient addition time <= brewing time
Time: O(n log n) Space: O(n)
A more efficient approach is to use a priority queue to manage the brewing tasks, allowing for a time complexity of O(n log n). This can be achieved by ordering the brewing tasks based on their brewing times and ingredient addition times.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

import sys from collections import deque def solve(brewingTasks): brewingTasks.sort(key=lambda x: x[0]) return [task[0] for task in brewingTasks] input = sys.stdin.readlines() input = [line.strip() for line in input] brewingTasks = [] for task in input: brewingTime, numIngredients, *ingredients = map(int, task.split()) brewingTasks.append((brewingTime, ingredients)) result = solve(brewingTasks) print(' '.join(map(str, result)))