Problem
FAANG · LeetCode #525

Contiguous Array — three-stage drill

1The ProblemMedium

Contiguous Array

Longest contiguous subarray with an equal number of 0s and 1s.

longest subarray with equal 0s and 1s00110203141506three 0s · three 1s — length 6return6
Examples
[0,1,0,0,1,1,0]→ 6[1,1,1]→ 0

constraintOnly 0s and 1s.

2The PatternTIME O(n) · SPACE O(n)

Prefix Sum

Running cumulative totals turn range-sum questions into O(1) lookups.

numsprefix2132420316283124sweep›prefix[i] = nums[0] + … + nums[i] → any range sum in O(1)
Problems using this pattern
3The SolutionRESULT → 6

Step through it

map 0 → −1, 1 → +1nums0100110balance0123456↩ ANSWERlongest =0balance → first indexseen = {0: -1}
start: seen = {0: -1} (balance 0 before the array), best = 0
Step 1 / 9
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent