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 ith wizard on the jth potion is timeij = skill[i] * mana[j].
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.
| Potion ID | Start Time | Wizard 0 Done Time | Wizard 1 Done Time | Wizard 2 Done Time | Wizard 3 Done Time |
|---|---|---|---|---|---|
| 0 | 0 | 5 | 30 | 40 | 60 |
| 1 | 52 | 53 | 58 | 60 | 64 |
| 2 | 54 | 58 | 78 | 86 | 102 |
| 3 | 86 | 88 | 98 | 102 | 110 |
As an example for why wizard 0 cannot start working on the 1st potion before time t = 52, consider the case where the wizards started preparing the 1st potion at time t = 50. At time t = 58, wizard 2 is done with the 1st potion, but wizard 3 will still be working on the 0th potion till time t = 60.
t = 0, and is completed by time t = 3.t = 1, and is completed by time t = 4.t = 2, and is completed by time t = 5.n == skill.lengthm == mana.length1 <= n, m <= 50001 <= mana[i], skill[i] <= 5000Maintain each wizard's earliest free time (for the last potion) as f[i].
Let x be the current mana value. Starting from now = f[0], update now = max(now + skill[i - 1] * x, f[i]) for i in [1..n]. Then, the final f[n - 1] = now + skill[n - 1] * x for this potion.
Update all other f values by f[i] = f[i + 1] - skill[i + 1] * x for i in [0..n - 2] (in reverse order).
No developer has claimed the Ruby throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Dynamic Programming.
# It may fail, but its not the problem of algorithm because same code can be accepted in other languages
# @param {Integer[]} skill
# @param {Integer[]} mana
# @return {Integer}
def min_time(skill, mana)
n = skill.size
m = mana.size
dp = Array.new(n, 0)
m.times do |i|
n.times do |j|
dp[j] = [dp[j], dp[j - 1]].max if j >= 1 # condition 1 and 2
time_consuming = mana[i] * skill[j]
dp[j] = [dp[j], dp[j + 1] - time_consuming].max if j < n - 1 # condition 3
dp[j] += time_consuming
end
# Process again from back to front to prevent any wizard from starting work too early.
(1...n).to_a.reverse.each do |j|
dp[j - 1] = dp[j] - mana[i] * skill[j]
end
end
dp[-1]
end
No developer has claimed the Python throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Dynamic Programming.
# It may fail, but its not the problem of algorithm because same code can be accepted in other languages
class Solution:
def minTime(self, skill: List[int], mana: List[int]) -> int:
n = len(skill)
m = len(mana)
dp = [0] * n
for i in range(m):
for j in range(n):
# condition 1 and 2
if j >= 1:
dp[j] = max(dp[j], dp[j - 1])
time_consuming = mana[i] * skill[j]
# condition 3
if j < n - 1:
dp[j] = max(dp[j], dp[j + 1] - time_consuming)
dp[j] += time_consuming
# Process again from back to front to prevent any wizard from starting work too early.
for j in range(n - 1, 0, -1):
dp[j - 1] = dp[j] - mana[i] * skill[j]
return dp[-1]
No developer has claimed the Javascript throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Dynamic Programming.
/**
* @param {number[]} skill
* @param {number[]} mana
* @return {number}
*/
var minTime = function (skill, mana) {
const n = skill.length;
const m = mana.length;
const dp = new Array(n).fill(0);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
// condition 1 and 2
if (j >= 1) {
dp[j] = Math.max(dp[j], dp[j - 1]);
}
const timeConsuming = mana[i] * skill[j];
// condition 3
if (j < n - 1) {
dp[j] = Math.max(dp[j], dp[j + 1] - timeConsuming);
}
dp[j] += timeConsuming;
}
// Process again from back to front to prevent any wizard from starting work too early.
for (let j = n - 1; j > 0; j--) {
dp[j - 1] = dp[j] - mana[i] * skill[j];
}
}
return dp[dp.length - 1];
};
No developer has claimed the Java throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Dynamic Programming.
class Solution {
public long minTime(int[] skill, int[] mana) {
int n = skill.length;
int m = mana.length;
long[] dp = new long[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// condition 1 and 2
if (j >= 1) {
dp[j] = Math.max(dp[j], dp[j - 1]);
}
long timeConsuming = (long) mana[i] * skill[j];
// condition 3
if (j < n - 1) {
dp[j] = Math.max(dp[j], dp[j + 1] - timeConsuming);
}
dp[j] += timeConsuming;
}
// Process again from back to front to prevent any wizard from starting work too
// early
for (int j = n - 1; j > 0; j--) {
dp[j - 1] = dp[j] - (long) mana[i] * skill[j];
}
}
return dp[n - 1];
}
}
No developer has claimed the C# throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Dynamic Programming.
public class Solution
{
public long MinTime(int[] skill, int[] mana)
{
int n = skill.Length;
int m = mana.Length;
long[] dp = new long[n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
// condition 1 and 2
if (j >= 1)
{
dp[j] = Math.Max(dp[j], dp[j - 1]);
}
long timeConsuming = (long)mana[i] * skill[j];
// condition 3
if (j < n - 1)
{
dp[j] = Math.Max(dp[j], dp[j + 1] - timeConsuming);
}
dp[j] += timeConsuming;
}
// Process again from back to front to prevent any wizard from starting work too early
for (int j = n - 1; j > 0; j--)
{
dp[j - 1] = dp[j] - (long)mana[i] * skill[j];
}
}
return dp[n - 1];
}
}
No developer has claimed the C++ throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Dynamic Programming.
class Solution {
public:
long long minTime(vector<int>& skill, vector<int>& mana) {
int n = skill.size();
int m = mana.size();
vector<long long> dp(n, 0);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
// condition 1 and 2
if (j >= 1) {
dp[j] = max(dp[j], dp[j - 1]);
}
long long time_consuming = (long long)mana[i] * skill[j];
// condition 3
if (j < n - 1) {
dp[j] = max(dp[j], dp[j + 1] - time_consuming);
}
dp[j] += time_consuming;
}
// Process again from back to front to prevent any wizard from
// starting work too early
for (int j = n - 1; j > 0; j--) {
dp[j - 1] = dp[j] - (long long)mana[i] * skill[j];
}
}
return dp[n - 1];
}
};
No developer has claimed the Go throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Dynamic Programming.
func minTime(skill []int, mana []int) int64 {
n := len(skill)
m := len(mana)
dp := make([]int64, n)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
// condition 1 and 2
if j >= 1 && dp[j-1] > dp[j] {
dp[j] = dp[j-1]
}
timeConsuming := int64(mana[i]) * int64(skill[j])
// condition 3
if j < n-1 {
if dp[j+1]-timeConsuming > dp[j] {
dp[j] = dp[j+1] - timeConsuming
}
}
dp[j] += timeConsuming
}
// Process again from back to front to prevent any wizard from starting work too early
for j := n - 1; j > 0; j-- {
dp[j-1] = dp[j] - int64(mana[i])*int64(skill[j])
}
}
return dp[n-1]
}
No developer has claimed the Rust throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #3494 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.