When : placing 0 keeps . Placing 1-9 sets and . When : only place digits within 1 of . Update for next position. Edge case: what's the first digit of 0? We define single-digit numbers as valid (no adjacent pair to check). Time: where . The constants are small. Space: for memoization table.
Time complexity: .
Space complexity: .