DDSA Solutions

3494. Find the Minimum Amount of Time to Brew Potions

Time: O(n * m)
Space: O(n)

Problem Overview

Find the Minimum Amount of Time to Brew Potions (Unknown) asks you to solve a structured algorithmic task. This is a common Array / Math pattern in coding interviews. Simulate brewing; each wizard starts when the previous mana assigned finishes.

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

Simulate brewing; each wizard starts when the previous mana assigned finishes.

Related patterns: Array, Math, Heap (Priority Queue)

3494.cs
C#
// Approach: Simulate brewing; each wizard starts when the previous mana assigned finishes.
// Time: O(n * m) Space: O(n)

public class Solution
{
    public long MinTime(int[] skill, int[] mana)
    {
        long sumSkill = skill.Sum();
        long prevWizardDone = sumSkill * mana[0];

        for (int j = 1; j < mana.Length; ++j)
        {
            long prevPotionDone = prevWizardDone;
            for (int i = skill.Length - 2; i >= 0; --i)
            {
                prevPotionDone -= (long)skill[i + 1] * mana[j - 1];
                prevWizardDone = Math.Max(prevPotionDone, prevWizardDone - (long)skill[i] * mana[j]);
            }
            prevWizardDone += sumSkill * mana[j];
        }

        return prevWizardDone;
    }
}
Was this solution helpful?

Related Problems