给定一个由N个整数组成的数组arr [] ,任务是找到需要删除的前缀的最小长度,以便可以重复地重新排列其余的数组元素,一个一个一个地选择最后一个元素或最后一个元素以形成排序对象。大批。
例子:
Input: arr[] = {6, 5, 4, 3, 4}
Output: 3
Explanation:
To make an array sorted according to the given condition, remove the first 3 elements.
After deletion, the array arr[] is modified to {3, 4}.
From the array arr[], selecting the first elements one by one, i.e. arr[0] -> arr[1], forms a sorted array {3, 4}.
Input: arr[] = {1, 3, 4, 2}
Output: 0
Explanation:
There is no need to delete any prefix.
By selecting array elements in the order arr[0] -> arr[3] -> arr[1] -> arr[2], forms a sorted array {1, 2, 3, 4}.
天真的方法:最简单的方法是从给定数组中删除前缀的所有可能长度,对于每个前缀,检查是否可以通过删除这些前缀来从其余数组元素中形成排序后的数组。如果发现是正确的,则打印删除的前缀的最小长度。
时间复杂度: 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)