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

📅  最后修改于: 2021-05-14 00:01:46             🧑  作者: 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]的点处停止。
  • 随着迭代的进行,保持递减的答案
  • 完成上述所有步骤后,打印索引值,该是必须删除的前缀的最小长度。

下面是上述方法的实现:

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


输出:
4






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