Imagine you have a sequence of numbers, like a combination lock or a digital counter. Given the current arrangement, you need to find the very next arrangement in lexicographical (dictionary) order.
A permutation is simply a rearrangement of elements. For example, [1,2,3] can be rearranged as [1,3,2], [2,1,3], [2,3,1], [3,1,2], or [3,2,1].
The Challenge: Given an array of integers, find the next lexicographically greater permutation. If no such permutation exists (you're at the highest possible arrangement), wrap around to the smallest permutation (sorted in ascending order).
Examples:
[1,2,3]→[1,3,2](next in dictionary order)[2,3,1]→[3,1,2](jump to next valid arrangement)[3,2,1]→[1,2,3](wrap around to smallest)
Constraints: You must modify the array in-place using only constant extra memory!
Input & Output
Constraints
- 1 ≤ nums.length ≤ 100
- 0 ≤ nums[i] ≤ 100
- Must modify the array in-place
- Use only O(1) extra memory space