📌  相关文章
📜  通过删除K长度子数组,最大和最小数组元素之间的差异最小

📅  最后修改于: 2021-04-17 14:33:58             🧑  作者: Mango

给定一个由N个整数和一个整数K组成的数组arr [] ,任务是在删除任何大小为K的子数组之后,找到数组中存在的最大元素和最小元素之间的最小差。

例子:

天真的方法:最简单的方法是一一删除所有大小为K的可能子数组,并计算其余元素之间的最大值和最小值之差。最后,将获得的最小差异列为prit。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:可以通过以下方法来优化上述方法存储最大最小前缀到任何索引,并存储从任何索引开始的最大和最小安全量。一旦计算出上述四个值,就以恒定的计算复杂度除去K长度子数组后,找到最大和最小数组元素

请按照以下步骤解决问题:

  • 初始化数组maxSufix []和minSuffix [] 。使得i maxSuffix []和minSuffix的第i个元素[]数组表示的最大值和最小值分别元素存在于i指数的权利。
  • 初始化两个变量,例如maxPrefixminPrefix,以存储存在于前缀子数组中的最大和最小元素。
  • 在索引[1,N]上遍历数组并检查i + K <=N 。如果发现为真,则执行以下步骤:
    • i索引开始删除大小为K的子数组后,数组中的最大值为max(maxSuffix [i + k],maxPrefix)。
    • i索引开始删除大小为K的子数组后,数组中的最小值为min(minSuffix [i + k],minPrefix)。
    • minDiff更新为min(minDiff,maximum-minimum) ,以存储最小差异。
  • minDiff打印为必需的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to minimize difference
// between maximum and minimum array
// elements by removing a K-length subarray
void minimiseDifference(vector& arr, int K)
{
    // Size of array
    int N = arr.size();
 
    // Stores the maximum and minimum
    // in the suffix subarray [i .. N-1]
    int maxSuffix[N + 1], minSuffix[N + 1];
 
    maxSuffix[N] = -1e9;
    minSuffix[N] = 1e9;
    maxSuffix[N - 1] = arr[N - 1];
    minSuffix[N - 1] = arr[N - 1];
 
    // Constructing the maxSuffix and
    // minSuffix arrays
 
    // Traverse the array
    for (int i = N - 2; i >= 0; --i) {
 
        maxSuffix[i] = max(
maxSuffix[i + 1],
 arr[i]);
        minSuffix[i] = min(
minSuffix[i + 1],
 arr[i]);
    }
 
    // Stores the maximum and minimum
    // in the prefix subarray [0 .. i-1]
    int maxPrefix = arr[0];
    int minPrefix = arr[0];
 
    // Store the minimum difference
    int minDiff = maxSuffix[K] - minSuffix[K];
 
    // Traverse the array
    for (int i = 1; i < N; ++i) {
 
        // If the suffix doesn't exceed
        // the end of the array
        if (i + K <= N) {
 
            // Store the maximum element
            // in array after removing
            // subarray of size K
            int maximum = max(maxSuffix[i + K], maxPrefix);
 
            // Stores the maximum element
            // in array after removing
            // subarray of size K
            int minimum = min(minSuffix[i + K], minPrefix);
 
            // Update minimum difference
            minDiff = min(minDiff, maximum - minimum);
        }
 
        // Updating the maxPrefix and
        // minPrefix with current element
        maxPrefix = max(maxPrefix, arr[i]);
        minPrefix = min(minPrefix, arr[i]);
    }
 
    // Print the minimum difference
    cout << minDiff << "\n";
}
 
// Driver Code
int main()
{
    vector arr = { 4, 5, 8, 9, 1, 2 };
    int K = 2;
    minimiseDifference(arr, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to minimize difference
  // between maximum and minimum array
  // elements by removing a K-length subarray
  static void minimiseDifference(int[] arr, int K)
  {
    // Size of array
    int N = arr.length;
 
    // Stores the maximum and minimum
    // in the suffix subarray [i .. N-1]
    int[] maxSuffix = new int[N + 1];
    int[] minSuffix = new int[N + 1];
 
    maxSuffix[N] = -1000000000;
    minSuffix[N] = 1000000000;
    maxSuffix[N - 1] = arr[N - 1];
    minSuffix[N - 1] = arr[N - 1];
 
    // Constructing the maxSuffix and
    // minSuffix arrays
 
    // Traverse the array
    for (int i = N - 2; i >= 0; --i) {
 
      maxSuffix[i]
        = Math.max(maxSuffix[i + 1], arr[i]);
      minSuffix[i]
        = Math.min(minSuffix[i + 1], arr[i]);
    }
 
    // Stores the maximum and minimum
    // in the prefix subarray [0 .. i-1]
    int maxPrefix = arr[0];
    int minPrefix = arr[0];
 
    // Store the minimum difference
    int minDiff = maxSuffix[K] - minSuffix[K];
 
    // Traverse the array
    for (int i = 1; i < N; ++i) {
 
      // If the suffix doesn't exceed
      // the end of the array
      if (i + K <= N) {
 
        // Store the maximum element
        // in array after removing
        // subarray of size K
        int maximum
          = Math.max(maxSuffix[i + K], maxPrefix);
 
        // Stores the maximum element
        // in array after removing
        // subarray of size K
        int minimum
          = Math.min(minSuffix[i + K], minPrefix);
 
        // Update minimum difference
        minDiff
          = Math.min(minDiff, maximum - minimum);
      }
 
      // Updating the maxPrefix and
      // minPrefix with current element
      maxPrefix = Math.max(maxPrefix, arr[i]);
      minPrefix = Math.min(minPrefix, arr[i]);
    }
 
    // Print the minimum difference
    System.out.print(minDiff);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 4, 5, 8, 9, 1, 2 };
    int K = 2;
    minimiseDifference(arr, K);
  }
}
 
// This code is contributed by susmitakundugoaldanga.


Python3
# Python 3 program for the above approach
 
# Function to minimize difference
# between maximum and minimum array
# elements by removing a K-length subarray
def minimiseDifference(arr, K):
   
    # Size of array
    N = len(arr)
 
    # Stores the maximum and minimum
    # in the suffix subarray [i .. N-1]
    maxSuffix = [0 for i in range(N + 1)]
    minSuffix = [0 for i in range(N + 1)]
 
    maxSuffix[N] = -1e9
    minSuffix[N] = 1e9
    maxSuffix[N - 1] = arr[N - 1]
    minSuffix[N - 1] = arr[N - 1]
 
    # Constructing the maxSuffix and
    # minSuffix arrays
 
    # Traverse the array
    i = N - 2
    while(i >= 0):
        maxSuffix[i] = max(maxSuffix[i + 1],arr[i])
        minSuffix[i] = min(minSuffix[i + 1], arr[i])
        i -= 1
 
    # Stores the maximum and minimum
    # in the prefix subarray [0 .. i-1]
    maxPrefix = arr[0]
    minPrefix = arr[0]
 
    # Store the minimum difference
    minDiff = maxSuffix[K] - minSuffix[K]
 
    # Traverse the array
    for i in range(1, N):
       
        # If the suffix doesn't exceed
        # the end of the array
        if (i + K <= N):
           
            # Store the maximum element
            # in array after removing
            # subarray of size K
            maximum = max(maxSuffix[i + K], maxPrefix)
 
            # Stores the maximum element
            # in array after removing
            # subarray of size K
            minimum = min(minSuffix[i + K], minPrefix)
 
            # Update minimum difference
            minDiff = min(minDiff, maximum - minimum)
 
        # Updating the maxPrefix and
        # minPrefix with current element
        maxPrefix = max(maxPrefix, arr[i])
        minPrefix = min(minPrefix, arr[i])
 
    # Print the minimum difference
    print(minDiff)
 
# Driver Code
if __name__ == '__main__':
    arr =  [4, 5, 8, 9, 1, 2]
    K = 2
    minimiseDifference(arr, K)
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFg {
 
  // Function to minimize difference
  // between maximum and minimum array
  // elements by removing a K-length subarray
  static void minimiseDifference(List arr, int K)
  {
    // Size of array
    int N = arr.Count;
 
    // Stores the maximum and minimum
    // in the suffix subarray [i .. N-1]
    int[] maxSuffix = new int[N + 1];
    int[] minSuffix = new int[N + 1];
 
    maxSuffix[N] = -1000000000;
    minSuffix[N] = 1000000000;
    maxSuffix[N - 1] = arr[N - 1];
    minSuffix[N - 1] = arr[N - 1];
 
    // Constructing the maxSuffix and
    // minSuffix arrays
 
    // Traverse the array
    for (int i = N - 2; i >= 0; --i) {
 
      maxSuffix[i]
        = Math.Max(maxSuffix[i + 1], arr[i]);
      minSuffix[i]
        = Math.Min(minSuffix[i + 1], arr[i]);
    }
 
    // Stores the maximum and minimum
    // in the prefix subarray [0 .. i-1]
    int maxPrefix = arr[0];
    int minPrefix = arr[0];
 
    // Store the minimum difference
    int minDiff = maxSuffix[K] - minSuffix[K];
 
    // Traverse the array
    for (int i = 1; i < N; ++i) {
 
      // If the suffix doesn't exceed
      // the end of the array
      if (i + K <= N) {
 
        // Store the maximum element
        // in array after removing
        // subarray of size K
        int maximum
          = Math.Max(maxSuffix[i + K], maxPrefix);
 
        // Stores the maximum element
        // in array after removing
        // subarray of size K
        int minimum
          = Math.Min(minSuffix[i + K], minPrefix);
 
        // Update minimum difference
        minDiff
          = Math.Min(minDiff, maximum - minimum);
      }
 
      // Updating the maxPrefix and
      // minPrefix with current element
      maxPrefix = Math.Max(maxPrefix, arr[i]);
      minPrefix = Math.Min(minPrefix, arr[i]);
    }
 
    // Print the minimum difference
    Console.WriteLine(minDiff);
  }
 
  // Driver Code
  public static void Main()
  {
    List arr
      = new List() { 4, 5, 8, 9, 1, 2 };
    int K = 2;
    minimiseDifference(arr, K);
  }
}
 
// This code is contributed by chitranayal.


输出
4

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