Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created October 9, 2025 18:51
Show Gist options
  • Select an option

  • Save tatsuyax25/beace5bce43ab77db660e20d39b52869 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/beace5bce43ab77db660e20d39b52869 to your computer and use it in GitHub Desktop.
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 prope
/**
* @param {number[]} skill
* @param {number[]} mana
* @return {number}
*/
var minTime = function(skill, mana) {
let m = mana.length; // Number of potions
let n = skill.length; // Number of wizards
// done[i] represents the time when wizard i is ready to start the next potion
// We use n+1 to simplify indexing (done[n] will hold the final result)
let done = new Array(n + 1).fill(0);
// Loop through each potion in order
for (let j = 0; j < m; j++) {
// Forward pass: compute brewing time for potion j across all wizards
for (let i = 0; i < n; i++) {
// Wizard i+1 starts potion j after both:
// - wizard i finishes potion j
// - wizard i+1 finishes previous potion
done[i + 1] = Math.max(done[i], done[i + 1]) + (skill[i] * mana[j]);
}
// Backward pass: reset done[i] to represent start time of wizard i for next potion
// This subtracts the time just added, leaving only the start time
for (let i = n - 1; i >= 0; i--) {
done[i] = done[i + 1] - (skill[i] * mana[j]);
}
}
// Final result: time when last wizard finishes last potion
return done[n];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment