Explanation for "Find number of subarrays that fit an exact criteria"?
402

Can anyone explain how the following structure is used for "Find number of subarrays that fit an exact criteria"?... I don't see it...

from collections import defaultdict

def fn(arr, k):
    counts = defaultdict(int)
    counts[0] = 1
    ans = curr = 0

    for num in arr:
        # do logic to change curr
        ans += counts[curr - k]
        counts[curr] += 1
    
    return ans
Comments (1)