< Summary

Information
Class: LeetCode.Algorithms.MaximumTotalDamageWithSpellCasting.MaximumTotalDamageWithSpellCastingDynamicProgrammingWithBinarySearch
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumTotalDamageWithSpellCasting\MaximumTotalDamageWithSpellCastingDynamicProgrammingWithBinarySearch.cs
Line coverage
100%
Covered lines: 59
Uncovered lines: 0
Coverable lines: 59
Total lines: 105
Line coverage: 100%
Branch coverage
100%
Covered branches: 14
Total branches: 14
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MaximumTotalDamage(...)100%1414100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\MaximumTotalDamageWithSpellCasting\MaximumTotalDamageWithSpellCastingDynamicProgrammingWithBinarySearch.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.MaximumTotalDamageWithSpellCasting;
 13
 14/// <inheritdoc />
 15public sealed class MaximumTotalDamageWithSpellCastingDynamicProgrammingWithBinarySearch : IMaximumTotalDamageWithSpellC
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n log n), where n is the length of power
 19    ///     Space complexity - O(m), where m is the number of unique spell damages
 20    /// </summary>
 21    /// <param name="power"></param>
 22    /// <returns></returns>
 23    public long MaximumTotalDamage(int[] power)
 224    {
 225        Array.Sort(power);
 26
 227        var n = power.Length;
 28
 229        Span<int> damages = stackalloc int[n];
 230        Span<int> counts = stackalloc int[n];
 31
 232        var uniqueCount = 0;
 33
 234        var current = power[0];
 235        var count = 1;
 36
 1637        for (var i = 1; i < n; i++)
 638        {
 639            if (power[i] == current)
 240            {
 241                count++;
 242            }
 43            else
 444            {
 445                damages[uniqueCount] = current;
 446                counts[uniqueCount] = count;
 447                uniqueCount++;
 48
 449                current = power[i];
 450                count = 1;
 451            }
 652        }
 53
 254        damages[uniqueCount] = current;
 255        counts[uniqueCount] = count;
 56
 257        uniqueCount++;
 58
 259        Span<long> total = stackalloc long[uniqueCount];
 60
 1661        for (var i = 0; i < uniqueCount; i++)
 662        {
 663            total[i] = (long)damages[i] * counts[i];
 664        }
 65
 266        Span<long> dp = stackalloc long[uniqueCount];
 67
 268        dp[0] = total[0];
 69
 1270        for (var i = 1; i < uniqueCount; i++)
 471        {
 472            var take = total[i];
 473            var skip = dp[i - 1];
 74
 475            var left = 0;
 476            var right = i - 1;
 477            var j = -1;
 78
 1079            while (left <= right)
 680            {
 681                var mid = left + ((right - left) / 2);
 82
 683                if (damages[mid] <= damages[i] - 3)
 384                {
 385                    j = mid;
 86
 387                    left = mid + 1;
 388                }
 89                else
 390                {
 391                    right = mid - 1;
 392                }
 693            }
 94
 495            if (j != -1)
 396            {
 397                take += dp[j];
 398            }
 99
 4100            dp[i] = Math.Max(skip, take);
 4101        }
 102
 2103        return dp[uniqueCount - 1];
 2104    }
 105}