bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1053. Previous Permutation With One Swap

1053. Previous Permutation With One Swap

Leetcode Solutions

Greedy Approach with Single Pass and Swap

  1. 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.
  2. If no such element is found, the array is already the smallest permutation, so return the original array.
  3. 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.
  4. Perform the swap between arr[i] and the found element.
  5. Return the modified array.
UML Thumbnail

Backtracking with All Possible Swaps

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...