FunctionRacerProblemsRace

Count Derangements

MediumDynamic programmingPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Dynamic programming problems