通过最多反转一个子数组来实现数组的字典最小排列
给定一个大小为N的数组arr[] ,它是从1 到 N的排列,任务是找到可以通过反转最多一个子数组形成的字典最小排列。
例子:
Input : arr[] = {1, 3, 4, 2, 5}
Output : 1 2 4 3 5
Explanation: The subarray from index 1 to index 3 can be reversed to get the lexicographically smallest permutation.
Input : arr[] = {4, 3, 1, 2}
Output: 1 3 4 2
方法:解决问题的思路是基于数组的遍历。
- 在给定的问题中,可以通过从左侧遍历并检查(i+1)是否等于arr[i]将最小数字放置在其正确位置一次反转来获得字典上最小的排列
- 如果不相等,则在数组中找到该i+1的索引,并将子数组从第 i 个索引反转到位置(i+1) 。
下面是上述方法的实现。
C++
// C++ code for the above approach
#include
using namespace std;
// Function to find the
// Lexicographically smallest
// Permutation by one subarray reversal
void lexsmallest(vector& arr, int n)
{
// Initialize the variables
// To store the first and last
// Position of the subarray
int first = -1, flag = 0, find = -1, last = -1;
// Traverse the array
// And check if arr[i]!=i+1
for (int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
// Mark the first position
// Of the Subarray to be reversed
first = i;
find = i + 1;
break;
}
}
// If flag == 0, it is the
// Smallest permutation,
// So print the array
if (flag == 0) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
// Check where the minimum element is present
else {
for (int i = 0; i < n; i++) {
// It is the last position
// Of the subarray to be
// Reversed
if (arr[i] == find) {
last = i;
break;
}
}
// Reverse the subarray
// And print the array
reverse(arr.begin() + first,
arr.begin() + last + 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
}
// Driver Code
int main()
{
// Initialize the array arr[]
vector arr = { 1, 3, 4, 2, 5 };
int N = arr.size();
// Function call
lexsmallest(arr, N);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to find the
// Lexicographically smallest
// Permutation by one subarray reversal
static void lexsmallest(int []arr, int n)
{
// Initialize the variables
// To store the first and last
// Position of the subarray
int first = -1, flag = 0, find = -1, last = -1;
// Traverse the array
// And check if arr[i]!=i+1
for (int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
// Mark the first position
// Of the Subarray to be reversed
first = i;
find = i + 1;
break;
}
}
// If flag == 0, it is the
// Smallest permutation,
// So print the array
if (flag == 0) {
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+ " ");
}
}
// Check where the minimum element is present
else {
for (int i = 0; i < n; i++) {
// It is the last position
// Of the subarray to be
// Reversed
if (arr[i] == find) {
last = i;
break;
}
}
// Reverse the subarray
// And print the array
arr = reverse(arr,first,last);
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+ " ");
}
}
}
static int[] reverse(int str[], int start, int end) {
// Temporary variable to store character
int temp;
while (start <= end) {
// Swapping the first and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return str;
}
// Driver Code
public static void main(String[] args)
{
// Initialize the array arr[]
int []arr = { 1, 3, 4, 2, 5 };
int N = arr.length;
// Function call
lexsmallest(arr, N);
}
}
// This code contributed by shikhasingrajput
Python3
# Python code for the above approach
# Function to find the
# Lexicographically smallest
# Permutation by one subarray reversal
def lexsmallest(arr, n):
# Initialize the variables
# To store the first and last
# Position of the subarray
first = -1
flag = 0
find = -1
last = -1
# Traverse the array
# And check if arr[i]!=i+1
for i in range(0, n):
if (arr[i] != i + 1):
flag = 1
# Mark the first position
# Of the Subarray to be reversed
first = i
find = i + 1
break
# If flag == 0, it is the
# Smallest permutation,
# So print the array
if (flag == 0):
for i in range(0, n):
print(arr[i], end=" ")
# Check where the minimum element is present
else:
for i in range(0, n):
# It is the last position
# Of the subarray to be
# Reversed
if (arr[i] == find):
last = i
break
# Reverse the subarray
# And print the array
arr[first: last + 1] = arr[first: last + 1][::-1]
print(*arr)
# Driver Code
# Initialize the array arr[]
arr = [1, 3, 4, 2, 5]
N = len(arr)
# Function call
lexsmallest(arr, N)
# This code is contributed by Samim Hossain Mondal.
C#
// C# code for the above approach
using System;
class GFG{
// Function to find the
// Lexicographically smallest
// Permutation by one subarray reversal
static void lexsmallest(int []arr, int n)
{
// Initialize the variables
// To store the first and last
// Position of the subarray
int first = -1, flag = 0, find = -1, last = -1;
// Traverse the array
// And check if arr[i]!=i+1
for (int i = 0; i < n; i++) {
if (arr[i] != i + 1) {
flag = 1;
// Mark the first position
// Of the Subarray to be reversed
first = i;
find = i + 1;
break;
}
}
// If flag == 0, it is the
// Smallest permutation,
// So print the array
if (flag == 0) {
for (int i = 0; i < n; i++) {
Console.Write(arr[i]+ " ");
}
}
// Check where the minimum element is present
else {
for (int i = 0; i < n; i++) {
// It is the last position
// Of the subarray to be
// Reversed
if (arr[i] == find) {
last = i;
break;
}
}
// Reverse the subarray
// And print the array
arr = reverse(arr,first,last);
for (int i = 0; i < n; i++) {
Console.Write(arr[i]+ " ");
}
}
}
static int[] reverse(int[] str, int start, int end) {
// Temporary variable to store character
int temp;
while (start <= end)
{
// Swapping the first and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return str;
}
// Driver Code
static public void Main (){
// Initialize the array arr[]
int [] arr = { 1, 3, 4, 2, 5 };
int N = arr.Length;
// Function call
lexsmallest(arr, N);
}
}
// This code is contributed by hrithikgarg03188.
Javascript
输出
1 2 4 3 5
时间复杂度: 在)
辅助空间: O(1)