Permutations
Generate all permutations of distinct integers Input: array of unique integers Output: all possible arrangements
Every position picks an unused element, then gives it back. I care more about the give-back than the pick.
Examples
You write
permute(nums: List[int])
Example 1
- Input
- nums = [1,2,3]
- returns
- [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Example 2
- Input
- nums = [0,1]
- returns
- [[0,1],[1,0]]
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsBacktracking
I reach for it when
- The question asks for all combinations, permutations or placements.
- You make a choice, explore it, and may need to take it back.
Say this before you type. Say the choice, the stopping rule, and the earliest moment you can prune.
All 15 Backtracking problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.