📌  相关文章
📜  要删除的最小前缀,以便可以重新排列剩余的数组以形成已排序的数组

📅  最后修改于: 2021-09-05 11:44:56             🧑  作者: Mango

给定一个由N个整数组成的数组arr[] ,任务是找到需要删除的前缀的最小长度,使得剩余的数组元素可以通过逐个选择第一个或最后一个元素来重复重新排列,形成一个排序数组。

例子:

朴素的方法:最简单的方法是从给定的数组中删除所有可能长度的前缀,对于每个前缀,检查是否可以通过删除这些前缀从剩余的数组元素形成一个排序的数组。如果发现为真,则打印删除的前缀的最小长度。

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

有效的方法:上述方法可以通过观察结果后缀的形式来优化arr[1] ≤ arr[2] ≤ … ≥ arr[N – 2] ≥ arr[N – 1]其中N是长度剩余数组和后缀的最大长度。请按照以下步骤操作:

  • 将变量索引初始化为N – 1
  • 从末尾遍历数组并在arr[index – 1] ≤ arr[index]处停止。
  • 随着迭代的进行,我们不断减少answer
  • 完成上述所有步骤后,打印索引的值,即必须删除的前缀的最小长度。

下面是上述方法的实现:

C++
// C++ program for above approach
 
#include 
using namespace std;
 
// Function to find the minimum length
// of prefix required to be deleted
int findMinLength(vector& arr)
{
    // Stores index to be returned
    int index = (int)arr.size() - 1;
 
    // Iterate until previous element
    // is <= current index
    while (index > 0
           && arr[index] >= arr[index - 1]) {
 
        // Decrementing index
        index--;
    }
 
    // Return index
    return index;
}
 
// Driver Code
int main()
{
 
    // Given arr[]
    vector arr = { 7, 8, 5, 0, -1,
                        -1, 0, 1, 2, 3, 4 };
 
    // Function Call
    cout << findMinLength(arr);
 
    return 0;
}


Java
// Java program for above approach
import java.util.*;
import java.io.*;
 
class GFG{
      
// Function to find the minimum length
// of prefix required to be deleted
static int findMinLength(int[] arr)
{
     
    // Stores index to be returned
    int index = (int)arr.length - 1;
   
    // Iterate until previous element
    // is <= current index
    while (index > 0 && arr[index] >=
                        arr[index - 1])
    {
         
        // Decrementing index
        index--;
    }
   
    // Return index
    return index;
}
 
// Driver code
public static void main(String args[])
{
     
    // Given arr[]
    int arr[]=  { 7, 8, 5, 0, -1,
                 -1, 0, 1, 2, 3, 4 };
    int n = arr.length;
     
    // Function call
    System.out.println(findMinLength(arr));
}
}
 
// This code is contributed by bikram2001jha


Python3
# Python3 program for above approach
 
# Function to find the minimum length
# of prefix required to be deleted
def findMinLength(arr):
   
    # Stores index to be returned
    index = len(arr) - 1;
 
    # Iterate until previous element
    # is <= current index
    while (index > 0 and arr[index] >= arr[index - 1]):
       
        # Decrementing index
        index -= 1;
 
    # Return index
    return index;
 
# Driver code
if __name__ == '__main__':
   
    # Given arr
    arr = [7, 8, 5, 0, -1,
           -1, 0, 1, 2, 3, 4];
    n = len(arr);
 
    # Function call
    print(findMinLength(arr));
 
# This code is contributed by Princi Singh


C#
// C# program for above approach
using System;
 
class GFG{
      
// Function to find the minimum length
// of prefix required to be deleted
static int findMinLength(int[] arr)
{
     
    // Stores index to be returned
    int index = (int)arr.Length - 1;
   
    // Iterate until previous element
    // is <= current index
    while (index > 0 && arr[index] >=
                        arr[index - 1])
    {
         
        // Decrementing index
        index--;
    }
     
    // Return index
    return index;
}
 
// Driver code
public static void Main(String []args)
{
     
    // Given []arr
    int []arr =  { 7, 8, 5, 0, -1,
                  -1, 0, 1, 2, 3, 4 };
                  
    int n = arr.Length;
     
    // Function call
    Console.WriteLine(findMinLength(arr));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
4

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

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