📜  通过反转后缀子数组可能的字典序最大排列

📅  最后修改于: 2021-09-08 12:26:02             🧑  作者: Mango

给定一个大小为N的数组arr[] 任务是通过反转数组中的任何后缀子数组来找到字典序最大的排列数组。

例子:

朴素的方法:最简单的方法是从数组中反转每个可能的后缀子数组,并打印出字典序最大可能的数组元素排列。
时间复杂度: O(N 2 )
辅助空间: O(N)

Efficient Approach:为了优化上述方法,思想是使用Greedy Approach。可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 初始化一个变量,比如flag-1 ,表示找到一个索引,其值小于最后一个元素。
  • 遍历给定的数组arr[]并在每次迭代中,检查arr[i] < arr[N – 1]是否。然后,将当前索引存储在变量flag 中并中断循环。
  • 完成上述步骤后,检查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
    // element 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
        // element 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
    # element 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;
 
    # Print the 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
    // element 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


Javascript


输出:
3 5 4 2 1

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live