Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active April 15, 2024 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unilecs/d9df24e45bd8752fbd03a24512e2ec19 to your computer and use it in GitHub Desktop.
Save unilecs/d9df24e45bd8752fbd03a24512e2ec19 to your computer and use it in GitHub Desktop.
Задача: Количество подмассивов, в которых максимальный элемент встречается не менее K раз
using System;
using System.Linq;
public class Program
{
public static long CountSubArrays(int[] nums, int k) {
int len = nums.Length;
int max = nums.Max();
int start = 0;
long res = 0; int maxCountInWindow = 0;
for (int end = 0; end < len; end++)
{
if (nums[end] == max)
maxCountInWindow++;
while (k == maxCountInWindow)
{
if (nums[start] == max)
{
maxCountInWindow--;
}
start++;
}
res += start;
}
return res;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(CountSubArrays(new int[] { 1, 3, 2, 3, 3 }, 2).ToString()); // 6
Console.WriteLine(CountSubArrays(new int[] { 1, 4, 2, 1 }, 3).ToString()); // 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment