| | 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.CountLargestGroup; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountLargestGroupFrequencyArray : ICountLargestGroup |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="n"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int CountLargestGroup(int n) |
| 2 | 24 | | { |
| 2 | 25 | | var count = 0; |
| 2 | 26 | | var max = 0; |
| | 27 | |
|
| 2 | 28 | | var frequencyArray = new int[37]; |
| | 29 | |
|
| 34 | 30 | | for (var i = 1; i <= n; i++) |
| 15 | 31 | | { |
| 15 | 32 | | var sum = 0; |
| | 33 | |
|
| 15 | 34 | | var num = i; |
| | 35 | |
|
| 34 | 36 | | while (num > 0) |
| 19 | 37 | | { |
| 19 | 38 | | sum += num % 10; |
| | 39 | |
|
| 19 | 40 | | num /= 10; |
| 19 | 41 | | } |
| | 42 | |
|
| 15 | 43 | | frequencyArray[sum]++; |
| | 44 | |
|
| 15 | 45 | | if (frequencyArray[sum] > max) |
| 3 | 46 | | { |
| 3 | 47 | | max = frequencyArray[sum]; |
| | 48 | |
|
| 3 | 49 | | count = 1; |
| 3 | 50 | | } |
| 12 | 51 | | else if (frequencyArray[sum] == max) |
| 12 | 52 | | { |
| 12 | 53 | | count++; |
| 12 | 54 | | } |
| 15 | 55 | | } |
| | 56 | |
|
| 2 | 57 | | return count; |
| 2 | 58 | | } |
| | 59 | | } |