Skip to content

Commit 079ceb3

Browse files
authored
Create count-subarrays-with-score-less-than-k.py
1 parent 49c8159 commit 079ceb3

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, two pointers
5+
class Solution(object):
6+
def countSubarrays(self, nums, k):
7+
"""
8+
:type nums: List[int]
9+
:type k: int
10+
:rtype: int
11+
"""
12+
result = total = left = 0
13+
for right in xrange(len(nums)):
14+
total += nums[right]
15+
while total*(right-left+1) >= k:
16+
total -= nums[left]
17+
left += 1
18+
result += right-left+1
19+
return result

0 commit comments

Comments
 (0)