All problems
Next Permutation
Problem
Return the next lexicographically greater permutation of the list nums, or the smallest permutation when nums is already the largest.
Find the rightmost ascent, swap its first element with the smallest larger value to its right, and reverse the suffix. Return a new list and handle duplicates.
Hint
Reversing the descending suffix turns it into the smallest possible tail.
Test cases
case 1
in: [[1,2,3]]
expected: [1,3,2]
case 2
in: [[3,2,1]]
expected: [1,2,3]
case 3
in: [[1,1,5]]
expected: [1,5,1]
case 4
in: [[1]]
expected: [1]
case 5
in: [[2,3,1]]
expected: [3,1,2]
Starter codePython
def next_permutation(nums):
# Your code here
pass5 test cases · run your code against them in the editor