-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
25 lines (25 loc) · 716 Bytes
/
Solution.cs
File metadata and controls
25 lines (25 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Solution
{
public long CountSubarrays(int[] nums, long k)
{
int n = nums.Length;
long[] prefixSum = new long[n + 1];
for (int i = 0; i < n; i++)
{
prefixSum[i + 1] = prefixSum[i] + nums[i];
}
long ret = 0;
int left = 0;
for (int right = 0; right < n; right++)
{
long score = (prefixSum[right + 1] - prefixSum[left]) * (right - left + 1);
while (left <= right && score >= k)
{
left++;
score = (prefixSum[right + 1] - prefixSum[left]) * (right - left + 1);
}
ret += right - left + 1;
}
return ret;
}
}