← Recursion & DP

Micro-Drill #99 — Permutation template

Recursion & DP Target: 15s

Permutation backtracking generates all orderings. Used in arrangement and scheduling problems.

def bt(cur):
    if len(cur) == n:
        res.append(cur[:])
        return
    for x in a:
        if x not in cur:
            cur.append(x)
            bt(cur)
            cur.pop()
bt([])

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #98 Micro #100 →