| | | 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.MinimumIncrementToMakeArrayUnique; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class MinimumIncrementToMakeArrayUniqueCounting : IMinimumIncrementToMakeArrayUnique |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n + k) |
| | | 19 | | /// Space complexity - O(n + k) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="nums"></param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public int MinIncrementForUnique(int[] nums) |
| | 6 | 24 | | { |
| | 6 | 25 | | var result = 0; |
| | | 26 | | |
| | 6 | 27 | | var frequency = new int[nums.Length + nums.Max()]; |
| | | 28 | | |
| | 106 | 29 | | foreach (var num in nums) |
| | 44 | 30 | | { |
| | 44 | 31 | | frequency[num]++; |
| | 44 | 32 | | } |
| | | 33 | | |
| | 144 | 34 | | for (var i = 0; i < frequency.Length; i++) |
| | 66 | 35 | | { |
| | 66 | 36 | | if (frequency[i] <= 1) |
| | 35 | 37 | | { |
| | 35 | 38 | | continue; |
| | | 39 | | } |
| | | 40 | | |
| | 31 | 41 | | var duplicates = frequency[i] - 1; |
| | | 42 | | |
| | 31 | 43 | | frequency[i + 1] += duplicates; |
| | | 44 | | |
| | 31 | 45 | | frequency[i] = 1; |
| | | 46 | | |
| | 31 | 47 | | result += duplicates; |
| | 31 | 48 | | } |
| | | 49 | | |
| | 6 | 50 | | return result; |
| | 6 | 51 | | } |
| | | 52 | | } |