Next Permutation - Complete Solution Guide
Next Permutation is LeetCode problem 31, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
A permutation of an array of integers is an arrangement of its members into a sequence or linear order. For example, for arr = [1,2,3] , the following are all the permutations of arr : [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1] . The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutati
Detailed Explanation
The problem asks us to find the next lexicographically greater permutation of a given array of integers. If no such greater permutation exists (i.e., the array is already in descending order), we need to rearrange the array into its smallest possible order (ascending order). The solution must be performed in-place, meaning we cannot use additional memory that scales with the size of the input array. In simpler terms, we need to rearrange the numbers in the array to form the next bigger number possible, treating the array as a single large number, and if no bigger number exists, sort it in ascending order.
Solution Approach
The solution involves four main steps. First, we scan the array from right to left to find the first element `nums[i]` that is smaller than its immediate right neighbor `nums[i+1]`. This element `nums[i]` is our 'pivot'. If no such element is found, it means the array is in descending order, and we reverse the entire array. Second, if a pivot is found, we search the subarray to the right of `nums[i]` for the smallest element that is greater than `nums[i]`. Third, we swap the pivot with this element. Finally, we reverse the subarray to the right of the pivot to create the smallest possible arrangement for the remaining elements.
Step-by-Step Algorithm
- Step 1: Start from the second last element (index n-2) and move towards the beginning of the array to find the first element nums[i] that is smaller than its next element nums[i+1]. If no such element is found (i becomes -1), the array is in descending order, and go to step 4.
- Step 2: If i >= 0 (a pivot is found), start from the end of the array and move towards i+1 to find the smallest element nums[j] that is greater than nums[i].
- Step 3: Swap nums[i] and nums[j].
- Step 4: Reverse the subarray from index i+1 to the end of the array (index n-1). If i was -1, this step reverses the entire array.
Key Insights
- Insight 1: The core idea is to find a decreasing sequence from the right. If the entire array is decreasing, it is the largest permutation, and we should reverse it to obtain the smallest permutation.
- Insight 2: To find the next greater permutation, we need to find the smallest element to the right of a pivot that is greater than the pivot, and then swap them.
- Insight 3: Reversing the sequence to the right of the pivot ensures that the remaining part of the number is as small as possible, resulting in the next lexicographical permutation.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Two Pointers.
Companies
Asked at: Accenture, Adobe, Amazon, Apple, Arcesium, Bloomberg, ByteDance, DE Shaw, DoorDash, Goldman Sachs, HashedIn, IBM, Infosys, Intuit, J.P. Morgan, LinkedIn, Meta, Microsoft, Mitsogo, Nike, Paytm, Rubrik, ServiceNow, TikTok, Uber, Upstart, VMware, Yahoo, Zepto, Zoho, tcs.