Next Permutation
00:00
AmazonMicrosoft
Rearrange array to the lexicographically next greater permutation. If largest permutation, rearrange to smallest (sorted ascending). In-place O(1) space.
Examples
Input → [1,2,3]
Output → [1,3,2]
Input → [3,2,1]
Output → [1,2,3]
Note: wrap around
Input → [1,1,5]
Output → [1,5,1]