Count Derangements
A derangement of n items is a rearrangement in which no item stays in its original position. Return how many derangements exist for n items. (For n = 0 the answer is 1.)
Example 1
n 3
returns 2
[2,3,1] and [3,1,2].
Constraints
0 <= n <= 8
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Dynamic programming problems
- Count the Free PlotsMedium
- Dice Sum WaysMedium
- Fairest Stone SplitMedium
- Fewest Perfect SquaresMedium
- Gems That ClashMedium
- Hands That Hit the TargetMedium
- Keystrokes to TransformMedium
- Knight on the KeypadMedium