3494. Find the Minimum Amount of Time to Brew Potions #2274
-
|
Topics: You are given two integer arrays, In a laboratory, 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 the potions to be brewed properly. Example 1:
As an example for why wizard 0 cannot start working on the 1ˢᵗ potion before time Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to find the minimum time to brew all potions when they must pass through all wizards sequentially and each wizard can only work on one potion at a time. The key insight is that for each potion, we need to find the earliest start time such that when it reaches each wizard, that wizard has finished their previous potion. Approach:
Let's implement this solution in PHP: 3494. Find the Minimum Amount of Time to Brew Potions <?php
/**
* @param Integer[] $skill
* @param Integer[] $mana
* @return Integer
*/
function minTime($skill, $mana) {
$n = count($skill);
$m = count($mana);
// Prefix sum of skills - time needed for first i wizards
$prefix = array_fill(0, $n + 1, 0);
for ($i = 1; $i <= $n; $i++) {
$prefix[$i] = $prefix[$i - 1] + $skill[$i - 1];
}
// Track earliest available time for each wizard
$freeTime = array_fill(0, $n, 0);
foreach ($mana as $x) {
// Find earliest start time for current potion
$start = $freeTime[0];
// Check constraints from all subsequent wizards
for ($i = 1; $i < $n; $i++) {
// Potion reaches wizard i at: start + prefix[i] * x
// Wizard i becomes free at: freeTime[i]
// We need: start + prefix[i] * x >= freeTime[i]
$requiredStart = $freeTime[$i] - $prefix[$i] * $x;
if ($requiredStart > $start) {
$start = $requiredStart;
}
}
// Update free times for all wizards
for ($i = 0; $i < $n; $i++) {
$freeTime[$i] = $start + $prefix[$i + 1] * $x;
}
}
return $freeTime[$n - 1];
}
// Test cases
echo minTime([1,5,2,4], [5,1,4,2]) . PHP_EOL; // Output: 110
echo minTime([1,1,1], [1,1,1]) . PHP_EOL; // Output: 5
echo minTime([1,2,3,4], [1,2]) . PHP_EOL; // Output: 21
?>Explanation:
Time Complexity: O(n × m) - we process each potion and for each, check all wizards This approach efficiently schedules potions while respecting the constraints that each wizard can only work on one potion at a time and potions must pass sequentially through all wizards. |
Beta Was this translation helpful? Give feedback.
We need to find the minimum time to brew all potions when they must pass through all wizards sequentially and each wizard can only work on one potion at a time.
The key insight is that for each potion, we need to find the earliest start time such that when it reaches each wizard, that wizard has finished their previous potion.
Approach:
Let's implement this solution in PHP: 3494. Find the Minimum Amount of Time to Brew Potions