1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/cs/count-subarrays-with-fixed-bounds.cs
2024-03-31 16:36:42 +02:00

28 lines
631 B
C#

public class Solution {
public long CountSubarrays(int[] nums, int minK, int maxK) {
long count = 0;
var (minIdx, maxIdx) = (-1, -1);
for (int i = 0, j = 0; j < nums.Length; ++j) {
var x = nums[j];
// is not bounded
if (x < minK || x > maxK) {
i = j + 1;
continue;
}
if (x == minK) {
minIdx = j;
}
if (x == maxK) {
maxIdx = j;
}
count += Math.Max(0, Math.Min(minIdx, maxIdx) - i + 1);
}
return count;
}
}