| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 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.MaximumBeautyOfAnArrayAfterApplyingOperation; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MaximumBeautyOfAnArrayAfterApplyingOperationDifferenceArray : IMaximumBeautyOfAnArrayAfterApplyingOperation |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + m) |
| | 19 | | /// Space complexity - O(n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="nums"></param> |
| | 22 | | /// <param name="k"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int MaximumBeauty(int[] nums, int k) |
| 2 | 25 | | { |
| 2 | 26 | | if (nums.Length == 1) |
| 0 | 27 | | { |
| 0 | 28 | | return 1; |
| | 29 | | } |
| | 30 | |
|
| 2 | 31 | | var maxBeauty = 0; |
| 2 | 32 | | var maxValue = nums.Max(); |
| | 33 | |
|
| 2 | 34 | | var count = new int[maxValue + 2]; |
| | 35 | |
|
| 22 | 36 | | foreach (var num in nums) |
| 8 | 37 | | { |
| 8 | 38 | | count[Math.Max(num - k, 0)]++; |
| 8 | 39 | | count[Math.Min(num + k + 1, maxValue + 1)]--; |
| 8 | 40 | | } |
| | 41 | |
|
| 2 | 42 | | var currentSum = 0; |
| | 43 | |
|
| 28 | 44 | | foreach (var val in count) |
| 11 | 45 | | { |
| 11 | 46 | | currentSum += val; |
| 11 | 47 | | maxBeauty = Math.Max(maxBeauty, currentSum); |
| 11 | 48 | | } |
| | 49 | |
|
| 2 | 50 | | return maxBeauty; |
| 2 | 51 | | } |
| | 52 | | } |