📌  相关文章
📜  要删除的最小子数组的长度,以便对剩余的数组进行排序

📅  最后修改于: 2021-10-26 05:50:33             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是打印要从arr[] 中删除的最小子数组的长度,以便对剩余的数组进行排序。

例子:

处理方法可以通过以下三种方法来去除子数组来解决问题:

  1. 从左到右遍历arr[] ,找到第一个左边的索引arr[left] > arr[left + 1] 。所以为了使数组排序而要删除的子数组长度是N-left-1。
  2. 如果left == N – 1 ,则该数组已排序,因此返回 0。
  3. 从右到左遍历arr[] ,找到第一个索引right ,即arr[right] < arr[right – 1] 。所以为了使数组排序而需要去除的子数组长度是正确的。
  4. 现在初始化一个变量mincount并取N-left-1right 中的最小值
  5. 现在计算要从数组中间删除的子数组的最小长度:
    1. i = 0, j = right 。并检查是否可以通过比较arr[i]arr[j]删除ij (不包括)之间的元素。
    2. 如果arr[j] >= arr[i] ,尝试使用j – i – 1更新答案并增加i以收紧窗口。
    3. 如果arr[j] < arr[i] ,则不能删除其间的元素,因此增加j以松开窗口。
    4. 遍历循环直到i > leftj == N 。并更新答案。
  6. 返回mincount

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Find the length of the shortest subarray
int findLengthOfShortestSubarray(int arr[], int N)
{
     
    // To store the result
    int minlength = INT_MAX;
 
    int left = 0;
    int right = N - 1;
 
    // Calculate the possible length of
    // the sorted subarray from left
    while (left < right &&
       arr[left + 1] >= arr[left])
    {
        left++;
    }
 
    // Array is sorted
    if (left == N - 1)
        return 0;
 
    // Calculate the possible length of
    // the sorted subarray from left
    while (right > left &&
       arr[right - 1] <= arr[right])
    {
        right--;
    }
 
    // Update the result
    minlength = min(N - left - 1, right);
 
    // Calculate the possible length
    // in the middle we can delete
    // and update the result
    int j = right;
    for(int i = 0; i < left + 1; i++)
    {
        if (arr[i] <= arr[j])
        {
             
            // Update the result
            minlength = min(minlength, j - i - 1);
        }
        else if (j < N - 1)
        {
            j++;
        }
        else
        {
            break;
        }
    }
 
    // Return the result
    return minlength;
}
 
// Driver Code
int main()
{
    int arr[] = { 6, 3, 10, 11, 15,
                  20, 13, 3, 18, 12 };
    int N = sizeof(arr) / sizeof(arr[0]);
     
    // Function call
    cout << (findLengthOfShortestSubarray(arr, N));
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java program for the
// above approach
import java.util.*;
class GFG {
 
// Find the length of the shortest subarray
static int findLengthOfShortestSubarray(int arr[],
                                        int N)
{
  // To store the result
  int minlength = Integer.MAX_VALUE;
 
  int left = 0;
  int right = N - 1;
 
  // Calculate the possible length of
  // the sorted subarray from left
  while (left < right &&
         arr[left + 1] >= arr[left])
  {
    left++;
  }
 
  // Array is sorted
  if (left == N - 1)
    return 0;
 
  // Calculate the possible length of
  // the sorted subarray from left
  while (right > left &&
         arr[right - 1] <= arr[right])
  {
    right--;
  }
 
  // Update the result
  minlength = Math.min(N - left - 1,
                       right);
 
  // Calculate the possible length
  // in the middle we can delete
  // and update the result
  int j = right;
   
  for (int i = 0; i < left + 1; i++)
  {
    if (arr[i] <= arr[j])
    {
      // Update the result
      minlength = Math.min(minlength,
                           j - i - 1);
    }
    else if (j < N - 1)
    {
      j++;
    }
    else
    {
      break;
    }
  }
 
  // Return the result
  return minlength;
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {6, 3, 10, 11, 15,
               20, 13, 3, 18, 12};
  int N = arr.length;
 
  // Function call
  System.out.print(
         (findLengthOfShortestSubarray(arr, N)));
}
}
 
// This code is contributed by Chitranayal


Python3
# Python3 program for the above approach
import sys
 
# Find the length of the shortest subarray
def findLengthOfShortestSubarray(arr):
 
    # To store the result
    minlength = sys.maxsize
 
    left = 0
    right = len(arr) - 1
 
    # Calculate the possible length of
    # the sorted subarray from left
    while left < right and arr[left + 1] >= arr[left]:
        left += 1
 
    # Array is sorted
    if left == len(arr) - 1:
        return 0
 
    # Calculate the possible length of
    # the sorted subarray from left
    while right > left and arr[right-1] <= arr[right]:
        right -= 1
 
    # Update the result
    minlength = min(len(arr) - left - 1, right)
 
    # Calculate the possible length
    # in the middle we can delete
    # and update the result
    j = right
    for i in range(left + 1):
 
        if arr[i] <= arr[j]:
 
            # Update the result
            minlength = min(minlength, j - i - 1)
 
        elif j < len(arr) - 1:
 
            j += 1
 
        else:
 
            break
 
    # Return the result
    return minlength
 
 
# Driver Code
arr = [6, 3, 10, 11, 15, 20, 13, 3, 18, 12]
 
# Function Call
print(findLengthOfShortestSubarray(arr))


C#
// C# program for the
// above approach
using System;
 
class GFG {
 
// Find the length of the shortest subarray
static int findLengthOfShortestSubarray(int []arr,
                                        int N)
{
   
  // To store the result
  int minlength = int.MaxValue;
 
  int left = 0;
  int right = N - 1;
 
  // Calculate the possible length of
  // the sorted subarray from left
  while (left < right &&
         arr[left + 1] >= arr[left])
  {
    left++;
  }
 
  // Array is sorted
  if (left == N - 1)
    return 0;
 
  // Calculate the possible length of
  // the sorted subarray from left
  while (right > left &&
         arr[right - 1] <= arr[right])
  {
    right--;
  }
 
  // Update the result
  minlength = Math.Min(N - left - 1,
                       right);
 
  // Calculate the possible length
  // in the middle we can delete
  // and update the result
  int j = right;
   
  for(int i = 0; i < left + 1; i++)
  {
    if (arr[i] <= arr[j])
    {
       
      // Update the result
      minlength = Math.Min(minlength,
                           j - i - 1);
    }
    else if (j < N - 1)
    {
      j++;
    }
    else
    {
      break;
    }
  }
 
  // Return the result
  return minlength;
}
 
// Driver Code
public static void Main(String[] args)
{
  int []arr = { 6, 3, 10, 11, 15,
                20, 13, 3, 18, 12 };
  int N = arr.Length;
 
  // Function call
  Console.Write(findLengthOfShortestSubarray(
                 arr, N));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
8

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程