题解 | 小红统计区间(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)

全部评论

相关推荐

LastWh1spe...:ssob真有些人和那个没睡醒一样
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 17:04
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务