1
var getMinSwaps = function (num, k) {2
const digits = [...num];3
const len = digits.length;5
// helper function to swap elements in digits in place6
const swap = (i, j) => ([digits[i], digits[j]] = [digits[j], digits[i]]);8
// helper function to reverse elements in digits from i to the end of digits9
const reverse = (i) => {10
for (let j = len - 1; i < j; ++i && --j) {15
// helper to get the next smallest permutation for digits16
const nextPermutation = () => {17
// from right to left, find the first decreasing index18
// in digits and store it as i20
while (digits[i] >= digits[i + 1]) {24
// from right to left, find the first index in digits25
// that is greater than element at i27
while (digits[j] <= digits[i]) {31
// swap the 2 elements at i and j33
// reverse all elements after i because we know that34
// all elements after i are in ascending order39
// find the next permutation k times40
for (let i = 0; i < k; i++) {44
// find out how many swaps it will take to get to the45
// kth permutation by finding out how many swaps46
// it takes to put digits back to its original state48
for (let i = 0; i < len; i++) {50
// find the first element in digits to matches51
// num[i] and hold its place at j52
while (num[i] !== digits[j]) {56
// move the element at j to i while counting the