dsa · medium

Next Permutation

GreyOrangeArrayTwo Pointers

Rearrange nums into the next lexicographical permutation. If already last, wrap to sorted ascending. Return the array.

Arguments

**Example:** [1,2,3][1,3,2]. [3,2,1][1,2,3].

Constraints

1 <= nums.length <= 100 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[1,2,3]

Expected:
[1,3,2]

Example 2

Input:
[3,2,1]

Expected:
[1,2,3]

Open in the Dojo editor