let arr = []; for (let i = 1; i <= n; i++) { arr.push(i); }
let prenum = function (arr, k) { if (k <= 1) { return arr; } let curpos = -1; let curval;
for (let i = n - 1; i >= 0; i--) { let cur = arr[i]; let pre = arr[i - 1]; if (cur > pre) { curpos = i - 1; curval = pre; break; } } if (curpos == -1) { arr = arr.sort(function (a, b) { return a - b; }); returnprenum(arr,k-1); } else { let newarr = arr.slice(curpos); newarr = newarr.sort(function (a, b) { return a - b; }); let twoval = arr.slice(0, curpos);
for (let i = 0; i < newarr.length; i++) { if (newarr[i] > curval) { twoval.push(...newarr.splice(i, 1)); break; } } twoval.push(...newarr); return() =>prenum(twoval, k - 1); }
}
returntrampolines(prenum)(arr,k).join(""); };
functiontrampolines (fn) { return(...args) => { let result = fn(...args) while (typeof result === 'function') result = result() return result } }