题解 | #罪犯转移#
罪犯转移
https://www.nowcoder.com/practice/b7b1ad820f0a493aa128ed6c9e0af448
对于给定罪行值数组nums,先找出nums前 c 项之和,然后使用滑动窗口进行迭代。
每次减去窗口左边的值,加上右边的值。如果中间的值之和不大于 t ,那么计数器ans增加1
输出 ans 即可
时间复杂度 O(n)
def solution(nums, n, t, c): if c > n: return 0 s = sum(nums[:c]) ans = 1 if s <= t else 0 for i in range(n - c): s += nums[i + c] - nums[i] ans += s <= t return ans while True: try: n, t, c = [int(x) for x in input().split()] nums = [int(x) for x in input().split()] print(solution(nums, n, t, c)) except: break