题解 | 小红统计区间(easy)
小红统计区间(easy)
https://www.nowcoder.com/practice/96e8db05848142808e69d04d604f2dd8?channelPut=tracker2
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
nums=list(map(int,input().split()))
n=len(nums)
l=0
total=0
ans=0
for r in range(n):
total+=nums[r]
while l<n and total-nums[l]>=k:
total-=nums[l]
l+=1
if total>=k:
ans+=l+1
print(ans)
不难看出,枚举右
时间复杂度O(n)
