Next Permutation
Rearrange nums into the lexicographically next greater permutation of its numbers, in place. If no greater permutation exists (the array is in descending order), rearrange it to the lowest possible order (ascending). Use only constant extra memory.
Open official problem prompt ↗Transform the array in place into the very next arrangement in dictionary order, wrapping to the smallest arrangement when it is already the largest.
Like counting up on an odometer of digits: you find the rightmost digit you can bump up minimally, replace it with the next-larger available digit, and reset everything to its right to the smallest possible tail.
- Input
- nums = [1, 2, 3]
- Output
- [1, 3, 2]
- Why
- Among all permutations of {1,2,3}, [1,3,2] is the smallest one strictly greater than [1,2,3].
1 <= nums.length <= 1000 <= nums[i] <= 100