给定长度为N的数组arr [] ,任务是从数组的末端删除最少数量的元素,以使数组不减少。只能从左端或右端删除元素。
例子:
Input: arr[] = {1, 2, 4, 1, 5}
Output: 2
We can’t make the array sorted after one removal.
But if we remove 2 elements from the right end, the
array becomes {1, 2, 4} which is sorted.
Input: arr[] = {3, 2, 1}
Output: 2
方法:解决此问题的一个非常简单的方法是找到给定数组中最长的非递减子数组的长度。假设长度为L。因此,需要删除的元素数将为N – L。使用本文讨论的方法可以很容易地找到最长的非递减子数组的长度。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the minimum number
// of elements to be removed from the ends
// of the array to make it sorted
int findMin(int* arr, int n)
{
// To store the final answer
int ans = 1;
// Two pointer loop
for (int i = 0; i < n; i++) {
int j = i + 1;
// While the array is increasing increment j
while (j < n and arr[j] >= arr[j - 1])
j++;
// Updating the ans
ans = max(ans, j - i);
// Updating the left pointer
i = j - 1;
}
// Returning the final answer
return n - ans;
}
// Driver code
int main()
{
int arr[] = { 3, 2, 1 };
int n = sizeof(arr) / sizeof(int);
cout << findMin(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the minimum number
// of elements to be removed from the ends
// of the array to make it sorted
static int findMin(int arr[], int n)
{
// To store the final answer
int ans = 1;
// Two pointer loop
for (int i = 0; i < n; i++)
{
int j = i + 1;
// While the array is increasing increment j
while (j < n && arr[j] >= arr[j - 1])
j++;
// Updating the ans
ans = Math.max(ans, j - i);
// Updating the left pointer
i = j - 1;
}
// Returning the final answer
return n - ans;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 3, 2, 1 };
int n = arr.length;
System.out.println(findMin(arr, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
# Function to return the minimum number
# of elements to be removed from the ends
# of the array to make it sorted
def findMin(arr, n):
# To store the final answer
ans = 1
# Two pointer loop
for i in range(n):
j = i + 1
# While the array is increasing increment j
while (j < n and arr[j] >= arr[j - 1]):
j += 1
# Updating the ans
ans = max(ans, j - i)
# Updating the left pointer
i = j - 1
# Returning the final answer
return n - ans
# Driver code
arr = [3, 2, 1]
n = len(arr)
print(findMin(arr, n))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the minimum number
// of elements to be removed from the ends
// of the array to make it sorted
static int findMin(int []arr, int n)
{
// To store the readonly answer
int ans = 1;
// Two pointer loop
for (int i = 0; i < n; i++)
{
int j = i + 1;
// While the array is increasing increment j
while (j < n && arr[j] >= arr[j - 1])
j++;
// Updating the ans
ans = Math.Max(ans, j - i);
// Updating the left pointer
i = j - 1;
}
// Returning the readonly answer
return n - ans;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 3, 2, 1 };
int n = arr.Length;
Console.WriteLine(findMin(arr, n));
}
}
// This code is contributed by Rajput-Ji
输出:
2
时间复杂度: O(N)
辅助空间: O(1)