Next Permutation
MediumTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.Think of all arrangements of a list of numbers sorted like words in a dictionary (lexicographic order). Given nums, rearrange it in place into the next greater arrangement. If nums is already the largest arrangement, rearrange it into the smallest one (sorted ascending). Use only O(1) extra memory and return nothing.
nums = [1, 2, 3] → nums becomes [1, 3, 2]
nums = [3, 2, 1] → nums becomes [1, 2, 3] (wraps around)
nums = [1, 1, 5] → nums becomes [1, 5, 1]Constraints
1 <= nums.length <= 1000 <= nums[i] <= 100