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
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check