Skip to content
Decoder

Next Permutation

Medium

Problem

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.

Example 1

Input
nums = [1, 2, 3]
Output
nums becomes [1, 3, 2]

Example 2

Input
nums = [3, 2, 1]
Output
nums becomes [1, 2, 3]
Why
wraps around

Example 3

Input
nums = [1, 1, 5]
Output
nums becomes [1, 5, 1]

Constraints

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.