📌  相关文章
📜  最多插入K后,最大程度地减少相邻元素的最大差异

📅  最后修改于: 2021-04-22 09:52:24             🧑  作者: Mango

给定一个由N个元素组成的数组,任务是通过在数组中最多插入K个元素来最小化相邻元素的最大差异。
例子:

方法:为了解决此问题,我们使用以下基于二进制搜索的方法:

  1. 找到数组中任何两个相邻元素之间的最大差,并将其存储在变量中,例如最差的
  2. 搜索从最佳(最初为1)到最差,并为每个中间值找到所需的插入次数。
  3. 每当插入数大于K的特定值mid时,在[mid + 1,最差] ,即较高的一半之间进行搜索。否则,在[best,mid-1] (即下半部分)之间进行搜索以检查最大差异是否可以在最多K次插入的情况下进一步最小化。
  4. 循环终止后的最终最差值给出了答案。

下面的代码是上述方法的实现:

C++
// C++ Program to find the minimum of maximum
// differerence between adjacent elements
// after at most K insertions
 
#include 
using namespace std;
 
int minMaxDiff(int arr[], int n, int k)
{
    int max_adj_dif = INT_MIN;
    // Calculate the maximum
    // adjacent difference
    for (int i = 0; i < n - 1; i++)
        max_adj_dif
            = max(max_adj_dif,
                  abs(arr[i] - arr[i + 1]));
 
    // If the maximum adjacent
    // difference is already zero
    if (max_adj_dif == 0)
        return 0;
 
    // best and worst specifies
    // range of the maximum
    // adjacent difference
    int best = 1;
    int worst = max_adj_dif;
    int mid, required;
 
    while (best < worst) {
 
        mid = (best + worst) / 2;
 
        // To store the no of insertions
        // required for respective
        // values of mid
        required = 0;
 
        for (int i = 0; i < n - 1; i++) {
 
            required += (abs(arr[i]
                             - arr[i + 1])
                         - 1)
                        / mid;
        }
 
        // If the number of insertions
        // required exceeds K
        if (required > k)
            best = mid + 1;
 
        // Otherwise
        else
            worst = mid;
    }
 
    return worst;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 12, 25, 50 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 7;
 
    cout << minMaxDiff(arr, n, k);
    return 0;
}


Java
// Java program to find the minimum
// of maximum difference between
// adjacent elements after at most
// K insertions
import java.util.*;
 
class GFG{
     
static int minMaxDiff(int arr[], int n, int k)
{
    int max_adj_dif = Integer.MIN_VALUE;
     
    // Calculate the maximum
    // adjacent difference
    for(int i = 0; i < n - 1; i++)
        max_adj_dif = Math.max(max_adj_dif,
                      Math.abs(arr[i] -
                               arr[i + 1]));
 
    // If the maximum adjacent
    // difference is already zero
    if (max_adj_dif == 0)
        return 0;
 
    // best and worst specifies
    // range of the maximum
    // adjacent difference
    int best = 1;
    int worst = max_adj_dif;
    int mid, required;
 
    while (best < worst)
    {
        mid = (best + worst) / 2;
 
        // To store the no of insertions
        // required for respective
        // values of mid
        required = 0;
 
        for(int i = 0; i < n - 1; i++)
        {
            required += (Math.abs(arr[i] -
                                  arr[i + 1]) -
                                     1) / mid;
        }
 
        // If the number of insertions
        // required exceeds K
        if (required > k)
            best = mid + 1;
 
        // Otherwise
        else
            worst = mid;
    }
    return worst;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 12, 25, 50 };
    int n = arr.length;
    int k = 7;
 
    System.out.println(minMaxDiff(arr, n, k));
}
}
 
// This code is contributed by ANKITKUMAR34


Python 3
# Python3 program to find the minimum
# of maximum difference between
# adjacent elements after at most
# K insertions
def minMaxDiff(arr, n, k):
 
    max_adj_dif = float('-inf');
     
    # Calculate the maximum
    # adjacent difference
    for i in range(n - 1):
        max_adj_dif = max(max_adj_dif,
                          abs(arr[i] -
                              arr[i + 1]));
 
    # If the maximum adjacent
    # difference is already zero
    if (max_adj_dif == 0):
        return 0;
 
    # best and worst specifies
    # range of the maximum
    # adjacent difference
    best = 1;
    worst = max_adj_dif;
     
    while (best < worst):
        mid = (best + worst) // 2;
 
        # To store the no of insertions
        # required for respective
        # values of mid
        required = 0
 
        for i in range(n - 1):
            required += (abs(arr[i] -
                             arr[i + 1]) - 1) // mid
             
        # If the number of insertions
        # required exceeds K
        if (required > k):
            best = mid + 1;
 
        # Otherwise
        else:
            worst = mid
 
    return worst
 
# Driver code
arr = [ 3, 12, 25, 50 ]
n = len(arr)
k = 7
 
print(minMaxDiff(arr, n, k))
 
# This code is contributed by ANKITKUMAR34


C#
// C# program to find the minimum
// of maximum difference between
// adjacent elements after at most
// K insertions
using System;
class GFG{
     
static int minMaxDiff(int []arr, int n, int k)
{
    int max_adj_dif = int.MinValue;
     
    // Calculate the maximum
    // adjacent difference
    for(int i = 0; i < n - 1; i++)
        max_adj_dif = Math.Max(max_adj_dif,
                      Math.Abs(arr[i] -
                               arr[i + 1]));
 
    // If the maximum adjacent
    // difference is already zero
    if (max_adj_dif == 0)
        return 0;
 
    // best and worst specifies
    // range of the maximum
    // adjacent difference
    int best = 1;
    int worst = max_adj_dif;
    int mid, required;
 
    while (best < worst)
    {
        mid = (best + worst) / 2;
 
        // To store the no of insertions
        // required for respective
        // values of mid
        required = 0;
 
        for(int i = 0; i < n - 1; i++)
        {
            required += (Math.Abs(arr[i] -
                                  arr[i + 1]) -
                                      1) / mid;
        }
 
        // If the number of insertions
        // required exceeds K
        if (required > k)
            best = mid + 1;
 
        // Otherwise
        else
            worst = mid;
    }
    return worst;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 3, 12, 25, 50 };
    int n = arr.Length;
    int k = 7;
 
    Console.WriteLine(minMaxDiff(arr, n, k));
}
}
 
// This code is contributed by Princi Singh


输出:
5