FAANG · LeetCode #454
4Sum II — three-stage drill
1The ProblemMedium
4Sum II
Count tuples (a,b,c,d), one from each array, summing to zero.
Examples
A=[1,2] B=[-2,-1] C=[-1,2] D=[0,2]→ 2
constraintFour arrays of equal length n; count index tuples, not values.
2The PatternTIME O(n) · SPACE O(n)
Arrays & Hashing
Trade space for time: a hash map turns 'have I seen this?' into an O(1) lookup.
Problems using this pattern
3The SolutionRESULT → 2
Step through it
build: 1 + (−2) = −1 → map[−1] = 1
Step 1 / 8
Arrow keys to navigate · escape to reset