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 permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
For example, the next permutation of arr = [1,2,3] is [1,3,2].
Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
Given an array of integers nums, find the next permutation of nums.
The replacement must be in place and use only constant extra memory.
public class codefile{
public static int[] build(int[] input){
int n = input.length;
int pivot = -1;
// Step 1: Find the rightmost pivot (breakpoint)
for (int i = n - 2; i >= 0; i--) {
if (input[i] < input[i + 1]) {
pivot = i;
break; // Fixed placement
}
}
// If no pivot, reverse the entire array
if (pivot == -1) {
reverse(input, 0, n - 1);
return input;
}
// Step 2: Find the next greater element and swap
for (int i = n - 1; i > pivot; i--) {
if (input[i] > input[pivot]) {
swap(input, i, pivot);
break;
}
}
// Step 3: Reverse the suffix (right side of pivot)
reverse(input, pivot + 1, n - 1);
return input;
}
// Function to swap elements
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Function to reverse array from index `start` to `end`
private static void reverse(int[] arr, int start, int end) {
while (start < end) {
swap(arr, start, end);
start++;
end--;
}
}
}
leetCode :
class Solution {
public void nextPermutation(int[] nums) {
int n = nums.length;
int ind = -1; // Step 1: Find the pivot (breakpoint)
// Find the rightmost index where nums[i] < nums[i + 1]
for (int i = n - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
ind = i;
break;
}
}
// If no pivot is found, the array is in descending order
if (ind == -1) {
reverse(nums, 0, n - 1); // Reverse entire array
return;
}
// Step 2: Find the next larger number to swap with nums[ind]
for (int i = n - 1; i > ind; i--) {
if (nums[i] > nums[ind]) {
swap(nums, i, ind);
break;
}
}
// Step 3: Reverse the suffix (right side of the array)
reverse(nums, ind + 1, n - 1);
}
// Utility function to swap two elements
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
// Utility function to reverse an array from index start to end
private void reverse(int[] nums, int start, int end) {
while (start < end) {
swap(nums, start, end);
start++;
end--;
}
}
}
Comments
Post a Comment