Last active
April 15, 2024 05:31
-
-
Save unilecs/d9df24e45bd8752fbd03a24512e2ec19 to your computer and use it in GitHub Desktop.
Задача: Количество подмассивов, в которых максимальный элемент встречается не менее K раз
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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