| | | 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 | | |
| | | 12 | | namespace LeetCode.Algorithms.FindTheMinimumAmountOfTimeToBrewPotions; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public 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) |
| | 3 | 25 | | { |
| | 3 | 26 | | var wizardCount = wizardSkills.Length; |
| | 3 | 27 | | var potionCount = potionMana.Length; |
| | | 28 | | |
| | 3 | 29 | | Span<long> prefixSkillSum = stackalloc long[wizardCount]; |
| | | 30 | | |
| | 22 | 31 | | for (var i = 1; i < wizardCount; i++) |
| | 8 | 32 | | { |
| | 8 | 33 | | prefixSkillSum[i] = prefixSkillSum[i - 1] + wizardSkills[i]; |
| | 8 | 34 | | } |
| | | 35 | | |
| | 3 | 36 | | var totalTime = (long)wizardSkills[0] * potionMana[0]; |
| | | 37 | | |
| | 18 | 38 | | for (var potionIndex = 1; potionIndex < potionCount; potionIndex++) |
| | 6 | 39 | | { |
| | 6 | 40 | | long previousMana = potionMana[potionIndex - 1]; |
| | 6 | 41 | | long currentMana = potionMana[potionIndex]; |
| | | 42 | | |
| | 6 | 43 | | var potionCompletionTime = wizardSkills[0] * currentMana; |
| | | 44 | | |
| | 44 | 45 | | for (var wizardIndex = 1; wizardIndex < wizardCount; wizardIndex++) |
| | 16 | 46 | | { |
| | 16 | 47 | | var totalPrevSkills = prefixSkillSum[wizardIndex]; |
| | 16 | 48 | | var totalPrevSkillsExcludingCurrent = prefixSkillSum[wizardIndex - 1]; |
| | | 49 | | |
| | 16 | 50 | | var wizardDelay = (totalPrevSkills * previousMana) - (totalPrevSkillsExcludingCurrent * currentMana); |
| | | 51 | | |
| | 16 | 52 | | potionCompletionTime = long.Max(potionCompletionTime, wizardDelay); |
| | 16 | 53 | | } |
| | | 54 | | |
| | 6 | 55 | | totalTime += potionCompletionTime; |
| | 6 | 56 | | } |
| | | 57 | | |
| | 3 | 58 | | totalTime += prefixSkillSum[wizardCount - 1] * potionMana[potionCount - 1]; |
| | | 59 | | |
| | 3 | 60 | | return totalTime; |
| | 3 | 61 | | } |
| | | 62 | | } |