< Summary

Information
Class: LeetCode.Algorithms.FindTheMinimumAmountOfTimeToBrewPotions.FindTheMinimumAmountOfTimeToBrewPotionsDynamicProgramming
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheMinimumAmountOfTimeToBrewPotions\FindTheMinimumAmountOfTimeToBrewPotionsDynamicProgramming.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 62
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MinTime(...)100%66100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\FindTheMinimumAmountOfTimeToBrewPotions\FindTheMinimumAmountOfTimeToBrewPotionsDynamicProgramming.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.FindTheMinimumAmountOfTimeToBrewPotions;
 13
 14/// <inheritdoc />
 15public sealed class FindTheMinimumAmountOfTimeToBrewPotionsDynamicProgramming : IFindTheMinimumAmountOfTimeToBrewPotions
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * m), where n is the length of wizardSkills and m is the length of potionMana
 19    ///     Space complexity - O(n), where n is the length of wizardSkills
 20    /// </summary>
 21    /// <param name="wizardSkills"></param>
 22    /// <param name="potionMana"></param>
 23    /// <returns></returns>
 24    public long MinTime(int[] wizardSkills, int[] potionMana)
 325    {
 326        var wizardCount = wizardSkills.Length;
 327        var potionCount = potionMana.Length;
 28
 329        Span<long> prefixSkillSum = stackalloc long[wizardCount];
 30
 2231        for (var i = 1; i < wizardCount; i++)
 832        {
 833            prefixSkillSum[i] = prefixSkillSum[i - 1] + wizardSkills[i];
 834        }
 35
 336        var totalTime = (long)wizardSkills[0] * potionMana[0];
 37
 1838        for (var potionIndex = 1; potionIndex < potionCount; potionIndex++)
 639        {
 640            long previousMana = potionMana[potionIndex - 1];
 641            long currentMana = potionMana[potionIndex];
 42
 643            var potionCompletionTime = wizardSkills[0] * currentMana;
 44
 4445            for (var wizardIndex = 1; wizardIndex < wizardCount; wizardIndex++)
 1646            {
 1647                var totalPrevSkills = prefixSkillSum[wizardIndex];
 1648                var totalPrevSkillsExcludingCurrent = prefixSkillSum[wizardIndex - 1];
 49
 1650                var wizardDelay = (totalPrevSkills * previousMana) - (totalPrevSkillsExcludingCurrent * currentMana);
 51
 1652                potionCompletionTime = long.Max(potionCompletionTime, wizardDelay);
 1653            }
 54
 655            totalTime += potionCompletionTime;
 656        }
 57
 358        totalTime += prefixSkillSum[wizardCount - 1] * potionMana[potionCount - 1];
 59
 360        return totalTime;
 361    }
 62}