Problem 388407 · medium · Phase 03 Linear Management & Searching

Windows Hitting the Target

sliding window · fixed-size window · counting

A dartboard game scores each throw in scores (negative scores are possible). A round consists of exactly k consecutive throws. Return how many rounds, i.e. windows of length k, have a total score exactly equal to target. Return 0 if k exceeds the number of throws.

Examples

Input:  scores = [1, 2, 1, 2, 1, 2], k = 2, target = 3
Output: 5
Explanation: every adjacent pair sums to 3.

Input:  scores = [5, 5, 5], k = 1, target = 5
Output: 3

Constraints

  • 0 <= len(scores) <= 10**5
  • 1 <= k <= 10**5, -10**4 <= scores[i] <= 10**4
  • Target complexity: O(n) time; summing each window separately is too slow for the largest tests.

Goals

  • Count fixed windows with an exact sum without re-summing
  • Handle negative values, which rule out shortcuts based on monotonic sums
Starting Python…