Leetcode Problem 1053. Previous Permutation With One Swap
1053. Previous Permutation With One Swap
Leetcode Solutions
Greedy Approach with Single Pass and Swap
Start from the second last element and move backwards to find the first element arr[i] that is greater than its next element arr[i+1]. This is the element we want to swap to make the permutation smaller.
If no such element is found, the array is already the smallest permutation, so return the original array.
Find the largest element to the right of arr[i] that is smaller than arr[i]. This is the element we want to swap with.
Perform the swap between arr[i] and the found element.