| | 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.CountSubarraysWhereMaxElementAppearsAtLeastKTimes; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class CountSubarraysWhereMaxElementAppearsAtLeastKTimesRecursive : |
| | 16 | | ICountSubarraysWhereMaxElementAppearsAtLeastKTimes |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Time complexity - O(n^2) |
| | 20 | | /// Space complexity - O(n^2) |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="nums"></param> |
| | 23 | | /// <param name="k"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public long CountSubarrays(int[] nums, int k) |
| 2 | 26 | | { |
| 2 | 27 | | var actualDuplicates = new List<List<int>>(); |
| | 28 | |
|
| 2 | 29 | | return CountSubarrays(nums, k, 0, nums.Length, actualDuplicates) + |
| 2 | 30 | | CountSubarrays(nums, k, 0, nums.Length, actualDuplicates); |
| 2 | 31 | | } |
| | 32 | |
|
| | 33 | | private static long CountSubarrays(IReadOnlyList<int> nums, int k, int left, int right, |
| | 34 | | ICollection<List<int>> actualDuplicates) |
| 380 | 35 | | { |
| 380 | 36 | | if (left > right) |
| 192 | 37 | | { |
| 192 | 38 | | return 0; |
| | 39 | | } |
| | 40 | |
|
| 188 | 41 | | var positionsDictionary = new Dictionary<int, int>(); |
| | 42 | |
|
| 188 | 43 | | var isSubarray = false; |
| | 44 | |
|
| 188 | 45 | | var currentSubarray = nums.Skip(left).Take(right - left).ToList(); |
| | 46 | |
|
| 692 | 47 | | for (var i = left; i < right; i++) |
| 164 | 48 | | { |
| 164 | 49 | | if (positionsDictionary.TryAdd(nums[i], 1)) |
| 146 | 50 | | { |
| 146 | 51 | | continue; |
| | 52 | | } |
| | 53 | |
|
| 18 | 54 | | positionsDictionary[nums[i]]++; |
| | 55 | |
|
| 64 | 56 | | if (positionsDictionary[nums[i]] != k || actualDuplicates.Any(l => l.SequenceEqual(currentSubarray))) |
| 12 | 57 | | { |
| 12 | 58 | | continue; |
| | 59 | | } |
| | 60 | |
|
| 6 | 61 | | actualDuplicates.Add(currentSubarray); |
| | 62 | |
|
| 6 | 63 | | isSubarray = true; |
| | 64 | |
|
| 6 | 65 | | break; |
| | 66 | | } |
| | 67 | |
|
| 188 | 68 | | var count = CountSubarrays(nums, k, left + 1, right, actualDuplicates) + |
| 188 | 69 | | CountSubarrays(nums, k, left, right - 1, actualDuplicates); |
| | 70 | |
|
| 188 | 71 | | if (isSubarray) |
| 6 | 72 | | { |
| 6 | 73 | | count++; |
| 6 | 74 | | } |
| | 75 | |
|
| 188 | 76 | | return count; |
| 380 | 77 | | } |
| | 78 | | } |