| | | 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.MaximumTotalDamageWithSpellCasting; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public 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) |
| | 2 | 24 | | { |
| | 2 | 25 | | Array.Sort(power); |
| | | 26 | | |
| | 2 | 27 | | var n = power.Length; |
| | | 28 | | |
| | 2 | 29 | | Span<int> damages = stackalloc int[n]; |
| | 2 | 30 | | Span<int> counts = stackalloc int[n]; |
| | | 31 | | |
| | 2 | 32 | | var uniqueCount = 0; |
| | | 33 | | |
| | 2 | 34 | | var current = power[0]; |
| | 2 | 35 | | var count = 1; |
| | | 36 | | |
| | 16 | 37 | | for (var i = 1; i < n; i++) |
| | 6 | 38 | | { |
| | 6 | 39 | | if (power[i] == current) |
| | 2 | 40 | | { |
| | 2 | 41 | | count++; |
| | 2 | 42 | | } |
| | | 43 | | else |
| | 4 | 44 | | { |
| | 4 | 45 | | damages[uniqueCount] = current; |
| | 4 | 46 | | counts[uniqueCount] = count; |
| | 4 | 47 | | uniqueCount++; |
| | | 48 | | |
| | 4 | 49 | | current = power[i]; |
| | 4 | 50 | | count = 1; |
| | 4 | 51 | | } |
| | 6 | 52 | | } |
| | | 53 | | |
| | 2 | 54 | | damages[uniqueCount] = current; |
| | 2 | 55 | | counts[uniqueCount] = count; |
| | | 56 | | |
| | 2 | 57 | | uniqueCount++; |
| | | 58 | | |
| | 2 | 59 | | Span<long> total = stackalloc long[uniqueCount]; |
| | | 60 | | |
| | 16 | 61 | | for (var i = 0; i < uniqueCount; i++) |
| | 6 | 62 | | { |
| | 6 | 63 | | total[i] = (long)damages[i] * counts[i]; |
| | 6 | 64 | | } |
| | | 65 | | |
| | 2 | 66 | | Span<long> dp = stackalloc long[uniqueCount]; |
| | | 67 | | |
| | 2 | 68 | | dp[0] = total[0]; |
| | | 69 | | |
| | 12 | 70 | | for (var i = 1; i < uniqueCount; i++) |
| | 4 | 71 | | { |
| | 4 | 72 | | var take = total[i]; |
| | 4 | 73 | | var skip = dp[i - 1]; |
| | | 74 | | |
| | 4 | 75 | | var left = 0; |
| | 4 | 76 | | var right = i - 1; |
| | 4 | 77 | | var j = -1; |
| | | 78 | | |
| | 10 | 79 | | while (left <= right) |
| | 6 | 80 | | { |
| | 6 | 81 | | var mid = left + ((right - left) / 2); |
| | | 82 | | |
| | 6 | 83 | | if (damages[mid] <= damages[i] - 3) |
| | 3 | 84 | | { |
| | 3 | 85 | | j = mid; |
| | | 86 | | |
| | 3 | 87 | | left = mid + 1; |
| | 3 | 88 | | } |
| | | 89 | | else |
| | 3 | 90 | | { |
| | 3 | 91 | | right = mid - 1; |
| | 3 | 92 | | } |
| | 6 | 93 | | } |
| | | 94 | | |
| | 4 | 95 | | if (j != -1) |
| | 3 | 96 | | { |
| | 3 | 97 | | take += dp[j]; |
| | 3 | 98 | | } |
| | | 99 | | |
| | 4 | 100 | | dp[i] = Math.Max(skip, take); |
| | 4 | 101 | | } |
| | | 102 | | |
| | 2 | 103 | | return dp[uniqueCount - 1]; |
| | 2 | 104 | | } |
| | | 105 | | } |