| | | 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.AlternatingGroups2; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class AlternatingGroups2BruteForce : IAlternatingGroups2 |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(n * k) |
| | | 19 | | /// Space complexity - O(1) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="colors"></param> |
| | | 22 | | /// <param name="k"></param> |
| | | 23 | | /// <returns></returns> |
| | | 24 | | public int NumberOfAlternatingGroups(int[] colors, int k) |
| | 3 | 25 | | { |
| | 3 | 26 | | var numberOfAlternatingGroups = 0; |
| | | 27 | | |
| | 38 | 28 | | for (var i = 0; i < colors.Length; i++) |
| | 16 | 29 | | { |
| | 16 | 30 | | var isAlternating = true; |
| | | 31 | | |
| | 92 | 32 | | for (var j = 0; j < k - 1; j++) |
| | 41 | 33 | | { |
| | 41 | 34 | | var curr = colors[(i + j) % colors.Length]; |
| | 41 | 35 | | var next = colors[(i + j + 1) % colors.Length]; |
| | | 36 | | |
| | 41 | 37 | | if (curr != next) |
| | 30 | 38 | | { |
| | 30 | 39 | | continue; |
| | | 40 | | } |
| | | 41 | | |
| | 11 | 42 | | isAlternating = false; |
| | | 43 | | |
| | 11 | 44 | | break; |
| | | 45 | | } |
| | | 46 | | |
| | 16 | 47 | | if (isAlternating) |
| | 5 | 48 | | { |
| | 5 | 49 | | numberOfAlternatingGroups++; |
| | 5 | 50 | | } |
| | 16 | 51 | | } |
| | | 52 | | |
| | 3 | 53 | | return numberOfAlternatingGroups; |
| | 3 | 54 | | } |
| | | 55 | | } |