Next Permutation
Medium · Arrays & Strings · about 20 minutes
Asked at Meta, Microsoft, Bloomberg, Amazon, Google.
The next permutation of an array is the next arrangement of its values in lexicographic order. If the array is already the largest arrangement (sorted descending), the next one wraps around to the smallest (sorted ascending). Rearrange nums into its next permutation in place with O(1) extra memory, and return it.