给定大小为N的数组arr [] ,任务是通过反转数组中的所有后缀子数组来找到词典上最大的排列数组。
例子:
Input: arr[] = {3, 5, 4, 1, 2}
Output: 3 5 4 2 1
Explanation: Reversing the suffix subarray {1, 2} generates the lexicographically largest permutation of the array elements possible.
Input: arr[] = {3, 5, 1, 2, 1}
Output: 3 5 1 2 1
Explanation:
The given array arr[] is already the lexicographically largest permuatation of the array possible.
天真的方法:最简单的方法是从数组中反转所有可能的后缀子数组,并打印在字典上可能最大的数组元素的排列。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效方法:为了优化上述方法,我们的想法是使用贪婪方法。可以根据以下观察结果解决给定的问题:
It can be observed that by choosing the suffix array as subarray in the range [i, N – 1] such that arr[i] < arr[N – 1] and reversing it, the obtained array will be lexicographically the largest array.
请按照以下步骤解决问题:
- 初始化一个变量,比如说标志为-1 ,表示找到一个索引,该索引的值小于最后一个元素。
- 遍历给定的数组arr [],并在每次迭代中检查arr [i]
是否正确。然后,将当前索引存储在变量标志中并中断循环。 - 完成上述步骤后,检查flag的值是否为-1 。如果发现为真,则反转后缀子数组,即[flag,N – 1]范围内的子数组。
- 完成上述步骤后,输出数组arr []作为结果。
下面是上述方法的实现:
C++
// CPP program for the above approach
#include
using namespace std;
// Function that
void LLA(vector A)
{
// Stores the index that have
// elemnent less than the
// element at last index
int flg = -1;
// Traverse the array
for (int i = 0; i < A.size(); i++)
{
// Checks if value at the
// current index is less
// than value at last index
if (A[i] < A[A.size() - 1])
{
// Assign the current
// index value to index
flg = i;
break;
}
}
// Check if index is not -1 then
// reverse the suffix from the
// index stored at flg
if (flg != -1)
{
// Reversal of suffix
for (int i = flg, j = A.size() - 1;
i <= j; i++, j--)
{
// Swapping Step
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
// Print the final Array
for (int i = 0; i < A.size(); i++)
cout< arr= { 3, 5, 4, 1, 2 };
// Function Call
LLA(arr);
}
// This code is contributed by mohit kumar 29.
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function that
public static void LLA(int A[])
{
// Stores the index that have
// elemnent less than the
// element at last index
int flg = -1;
// Traverse the array
for (int i = 0; i < A.length; i++) {
// Checks if value at the
// current index is less
// than value at last index
if (A[i] < A[A.length - 1]) {
// Assign the current
// index value to index
flg = i;
break;
}
}
// Check if index is not -1 then
// reverse the suffix from the
// index stored at flg
if (flg != -1) {
// Reversal of suffix
for (int i = flg, j = A.length - 1;
i <= j; i++, j--) {
// Swapping Step
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
// Print the final Array
for (int i = 0; i < A.length; i++)
System.out.print(A[i] + " ");
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 3, 5, 4, 1, 2 };
// Function Call
LLA(arr);
}
}
Python3
# Python program for the above approach
# Function that
def LLA(A):
# Stores the index that have
# elemnent less than the
# element at last index
flg = -1;
# Traverse the array
for i in range(len(A)):
# Checks if value at the
# current index is less
# than value at last index
if (A[i] < A[len(A) - 1]):
# Assign the current
# index value to index
flg = i;
break;
# Check if index is not -1 then
# reverse the suffix from the
# index stored at flg
if (flg != -1):
# Reversal of suffix
j = len(A) - 1;
for i in range(flg, j + 1):
# Swapping Step
temp = A[i];
A[i] = A[j];
A[j] = temp;
j -= 1;
# Prthe final Array
for i in range(len(A)):
print(A[i], end=" ");
# Driver Code
if __name__ == '__main__':
arr = [3, 5, 4, 1, 2];
# Function Call
LLA(arr);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
// Function that
public static void LLA(int []A)
{
// Stores the index that have
// elemnent less than the
// element at last index
int flg = -1;
// Traverse the array
for (int i = 0; i < A.Length; i++)
{
// Checks if value at the
// current index is less
// than value at last index
if (A[i] < A[A.Length - 1])
{
// Assign the current
// index value to index
flg = i;
break;
}
}
// Check if index is not -1 then
// reverse the suffix from the
// index stored at flg
if (flg != -1)
{
// Reversal of suffix
for (int i = flg, j = A.Length - 1;
i <= j; i++, j--)
{
// Swapping Step
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
// Print the readonly Array
for (int i = 0; i < A.Length; i++)
Console.Write(A[i] + " ");
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 5, 4, 1, 2 };
// Function Call
LLA(arr);
}
}
// This code is contributed by 29AjayKumar
3 5 4 2 1
时间复杂度: O(N)
辅助空间: O(1)