A permutation of an array of integers is an arrangement of its members into a sequence or linear order. The next permutation of an array of integers is the next lexicographically greater permutation of its integer. If such an arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order). Given an array of integers nums, find the next permutation of nums. The replacement must be in place and use only constant extra memory.
Example 1
Input: nums = [1,2,3]
Output: [1,3,2]
Explanation: The permutations of [1,2,3] in order are 123, 132, 213, … The one right after [1,2,3] is [1,3,2].
Example 2
Input: nums = [3,2,1]
Output: [1,2,3]
Explanation: [3,2,1] is the largest possible order, so it wraps around to the smallest, [1,2,3].
Example 3
Input: nums = [1,1,5]
Output: [1,5,1]
Explanation: The arrangements of [1,1,5] in order are 115, 151, 511. The next one after [1,1,5] is [1,5,1].
Scan from the right for the first descent (the pivot). Swap it with the smallest element to its right that is still larger, then reverse the suffix to make it as small as possible.
Run checks all cases above. Submit evaluates all test cases.