📌  相关文章
📜  最多插入 K 次后,最小化相邻元素的最大差异

📅  最后修改于: 2021-09-05 11:51:11             🧑  作者: Mango

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

方法:为了解决这个问题,我们使用以下基于二分搜索的方法:

  1. 找出数组中任意两个相邻元素之间的最大差值,并将其存储在一个变量中,比如最坏的
  2. 最佳(最初为 1)到最差搜索,并为每个中间值找到所需的插入次数。
  3. 对于特定的mid值,每当插入次数大于K 时,在[mid + 1,mostly]之间搜索,即较高的一半。否则在[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


Javascript


输出:
5

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live