/*
* @lc app=leetcode.cn id=1053 lang=typescript
*
* [1053] 交换一次的先前排列
*/
// @lc code=start
function prevPermOpt1(arr: number[]): number[] {}
// @lc code=end
function prevPermOpt1(arr: number[]): number[] {
const n = arr.length
for (let i = n - 1; i > 0; i--) {
if (arr[i] < arr[i - 1]) {
let t = i
for (let j = i + 1; j < n; j++) {
if (arr[i - 1] > arr[j]) {
if (arr[j] === arr[i]) continue
t = j
} else {
break
}
}
;[arr[i - 1], arr[t]] = [arr[t], arr[i - 1]]
break
}
}
return arr
}
test.each([
{ input: { arr: [3, 2, 1] }, output: [3, 1, 2] },
{ input: { arr: [1, 1, 5] }, output: [1, 1, 5] },
{ input: { arr: [1, 9, 4, 6, 7] }, output: [1, 7, 4, 6, 9] },
])('input: arr = $input.arr', ({ input: { arr }, output }) => {
expect(prevPermOpt1(arr)).toEqual(output)
})