Problem
FAANG · LeetCode #1

Two Sum — three-stage drill

1The ProblemEasy

Two Sum

Return indices of the two numbers that add up to a target.

find two indices summing to6302142nums[1] + nums[2] = 2 + 4 = 6return[1, 2]
Examples
[3,2,4], target=6→ [1,2][3,2,4], target=10→ none

constraintExactly one solution; can't use the same element twice.

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.

remember what you've seen in a hash map → O(1) lookup4021722344scan›value → countseen = {4: 1}
Problems using this pattern
3The SolutionRESULT → [1, 2]

Step through it

target = 6302142↩ ANSWERresult =?value → indexseen = {}
seen = {} (value → index). target = 6
Step 1 / 4
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent