cs: add «2444. Count Subarrays With Fixed Bounds»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
ec5319c3fb
commit
a59eaa2eb4
1 changed files with 28 additions and 0 deletions
28
cs/count-subarrays-with-fixed-bounds.cs
Normal file
28
cs/count-subarrays-with-fixed-bounds.cs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue