给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是在删除任何大小为K 的子数组后,找到数组中存在的最大和最小元素之间的最小差异。
例子:
Input: arr[] = {4, 5, 8, 9, 1, 2}, K = 2
Output: 4
Explanation: Remove the subarray {8, 9}. The minimum difference between maximum and minimum array elements becomes (5 – 1) = 4.
Input: arr[] = {1, 2, 2}, K = 1
Output: 0
Explanation: Remove subarray {1}. The minimum difference between maximum and minimum array elements becomes (2 – 2) = 0.
朴素方法:最简单的方法是将所有可能的大小为K 的子数组一一删除,并计算剩余元素之间的最大值和最小值之间的差值。最后,打印得到的最小差值。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:可以通过将最大和最小前缀存储到任何索引并存储从任何索引开始的最大和最小后缀来优化上述方法。计算出上述四个值后,求出计算复杂度为常数的K长子数组去除后的最大和最小数组元素。
请按照以下步骤解决问题:
- 初始化数组maxSufix[] 和 minSuffix[] 。这样 maxSuffix[] 和 minSuffix[] 数组的第i个元素表示分别出现在第i个索引右侧的最大和最小元素。
- 初始化两个变量,比如maxPrefix和minPrefix,以存储前缀子数组中存在的最大和最小元素。
- 在索引[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.
Javascript
4
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live