Start learning free

LESSON 322 OF 348 · DYNAMIC PROGRAMMING

Permutations with K Inversions in Scala

Practice O(n * k)

Given two non-negative integers N and K, return the number permutations of length N with exactly K inversions

An inversion is a pair of indices (i, j) with i < j and p(i) > p(j)

Example:

Input: N = 4, K = 2
Output: 5
Explanation: [
    [1, 3, 4, 2],
    [1, 4, 2, 3],
    [1, 4, 3, 2],
    [2, 1, 4, 3],
    [3, 1, 2, 4],
]
Notes:
N <= 12
K <= N * (N - 1)) / 2
STEP 1 OF 7
    Ctrl + Enter to check

    Permutations With K Inversions in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free