Skip to content

Commit 49c8159

Browse files
authored
Create count-subarrays-with-score-less-than-k.cpp
1 parent 9eacfde commit 49c8159

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// sliding window
5+
class Solution {
6+
public:
7+
long long countSubarrays(vector<int>& nums, long long k) {
8+
int64_t result = 0, total = 0;
9+
int left = 0;
10+
for (int right = 0; right < size(nums); ++right) {
11+
total += nums[right];
12+
while (total * (right - left + 1) >= k) {
13+
total -= nums[left++];
14+
}
15+
result += right - left + 1;
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)