FAANG · LeetCode #560
Subarray Sum Equals K — three-stage drill
1The ProblemMedium
Subarray Sum Equals K
Count contiguous subarrays whose sum equals k (values may be negative).
Examples
[4,2,-2,4,2], k=4→ 5[1,2,3], k=10→ 0
constraintValues may be negative.
2The PatternTIME O(n) · SPACE O(n)
Prefix Sum
Running cumulative totals turn range-sum questions into O(1) lookups.
Problems using this pattern
3The SolutionRESULT → 5
Step through it
start: seen = {0: 1}, count = 0
Step 1 / 7
Arrow keys to navigate · escape to reset