213. Next Permutation
Medium
Lexicographical Rearrangement
Array
Problem
Given an integer array nums representing a permutation, rearrange it into the lexicographically next greater permutation. If no greater permutation exists, rearrange it into the smallest possible order. Return the resulting array.
Examples
Example 1
Input: [1,2,3]
Output: [1,3,2]
Example 2
Input: [3,2,1]
Output: [1,2,3]
Example 3
Input: [1,1,5]
Output: [1,5,1]
Constraints
� 1 <= nums.length <= 100
� 0 <= nums[i] <= 100
Hints
?? Find the longest non-increasing suffix.
?? Swap the pivot with the smallest larger value in the suffix.
?? Reverse the suffix afterward.
Expected Complexity
Time: O(n)
Space: O(1)
Follow-up
Can you perform the transformation in-place using O(1) extra space?
Practice Notes
Attempts: 0
Time spent: 0min
Hints used: 0
JavaScript
00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,2,3]Expected
[1,3,2]