Given a positive integer `n`, find the smallest integer which has exactly the same digits existing in the integer `n` and is greater in value than `n`. If no such positive integer exists, return `-1`. Note that the returned integer should fit in **32-bit** integer, if there is a valid answer but it does not fit in **32-bit** integer, return `-1`.
Example 1
Input: n = 12
Output: 21
Explanation: 21 is the only rearrangement of {1,2} greater than 12.
Example 2
Input: n = 21
Output: -1
Explanation: Digits are fully descending; no larger arrangement exists.
Example 3
Input: n = 12443322
Output: 13222344
Explanation: Scanning from the right, the first digit smaller than the one after it is the 2 at index 1. Swap it with the smallest larger digit to its right (a 3) to get 13443222, then sort the digits after it in ascending order to get 13222344.
The next larger number with the same digits is exactly the 'next permutation' of the digit string. Find the rightmost digit smaller than its neighbour (the pivot), swap it with the smallest larger digit to its right, then reverse the suffix to make it as small as possible.
Run checks all cases above. Submit evaluates all test cases.