High-frequency question apparently, which made me more nervous than I should've been.
Scan from right to left to find the first index i where nums[i] > nums[i+1], as this is the point where a swap can create a smaller permutation. Then find the largest value to the right of i that is smaller than nums[i], choosing the rightmost occurrence if duplicates exist, and swap them. If no such i exists, the array is already the smallest permutation, so return it unchanged.
Pro tip: Emphasize that picking the rightmost occurrence of the largest smaller value ensures the result is the lexicographically largest possible after the swap. Also, mention that the algorithm runs in O(n) time and O(1) space, which is optimal.
Traverse the array from right to left and find the first index i where nums[i] > nums[i+1]. If no such index exists, the array is already the smallest permutation, so return it as is.
Among the elements to the right of i, find the largest value that is strictly less than nums[i]. If there are multiple occurrences, choose the rightmost one to maximize the resulting permutation.
Swap nums[i] with the chosen element. This yields the lexicographically largest permutation smaller than the original.
Return the modified array. If no swap was performed, return the original array.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.