Next permutation

Write codeMedium28 minGoogle and Meta style

Your task

Given an array of integers nums, rearrange it IN PLACE into the lexicographically next greater permutation of its elements. If no greater permutation exists (the array is in descending order), rearrange it to the lowest order (ascending). Return the resulting array. Must use O(1) extra space. Constraints: 1 <= len(nums) <= 10^4, 0 <= nums[i] <= 100.

Examples

next_permutation(nums: list[int]) -> list[int]
Example 1
Inputnext_permutation([1, 2, 3])
Output[1, 3, 2]