📌  相关文章
📜  使连续差异 <= K 所需的最少数组插入

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

给定一个表示建筑物高度的整数数组H和一个整数K 。任务是按照以下规则从第一个建筑物到达最后一个建筑物:

  1. 从高度为H i的建筑物到达高度为H j的建筑物只有在|H i – H j | 时才有可能。 <= K并且建筑物在数组中一个接一个地出现。
  2. 如果无法到达建筑物,则可以在两个建筑物之间插入许多中等高度的建筑物。

找出在第 2 步中完成的最小插入次数,以使从第一个建筑物到最后一个建筑物成为可能。
例子:

方法:

  • 运行从1 到 n-1的循环并检查 abs(H[i] – H[i-1]) <= K。
  • 如果上述条件为真,则跳到循环的下一次迭代。
  • 如果条件为假,则所需的插入将等于 ceil(diff / K) – 1 其中 diff = abs(H[i] – H[i-1])
  • 最后打印总插入量。

下面是上述方法的实现:

C++
// CPP implementation of above approach
#include 
using namespace std;
 
// Function to return minimum
// number of insertions required
int minInsertions(int H[], int n, int K)
{
    // Initialize insertions to 0
    int inser = 0;
 
    for (int i = 1; i < n; ++i) {
        float diff = abs(H[i] - H[i - 1]);
 
        if (diff <= K)
            continue;
        else
            inser += ceil(diff / K) - 1;
    }
 
    // return total insertions
    return inser;
}
 
// Driver program
int main()
{
    int H[] = { 2, 4, 8, 16 }, K = 3;
    int n = sizeof(H) / sizeof(H[0]);
    cout << minInsertions(H, n, K);
    return 0;
}


Java
// Java implementation of above approach
 
class GFG{
// Function to return minimum
// number of insertions required
static int minInsertions(int[] H, int n, int K)
{
    // Initialize insertions to 0
    int inser = 0;
 
    for (int i = 1; i < n; ++i) {
        float diff = Math.abs(H[i] - H[i - 1]);
 
        if (diff <= K)
            continue;
        else
            inser += Math.ceil(diff / K) - 1;
    }
 
    // return total insertions
    return inser;
}
 
// Driver program
public static void main(String[] args)
{
    int[] H = new int[]{ 2, 4, 8, 16 };
    int K = 3;
    int n = H.length;
    System.out.println(minInsertions(H, n, K));
}
}
// This code is contributed by mits


Python3
# Python3 implementation of above approach
import math
 
# Function to return minimum
# number of insertions required
def minInsertions(H, n, K):
 
    # Initialize insertions to 0
    inser = 0;
 
    for i in range(1, n):
        diff = abs(H[i] - H[i - 1]);
 
        if (diff <= K):
            continue;
        else:
            inser += math.ceil(diff / K) - 1;
 
    # return total insertions
    return inser;
 
# Driver Code
H = [2, 4, 8, 16 ];
K = 3;
n = len(H);
print(minInsertions(H, n, K));
 
# This code is contributed
# by mits


C#
// C# implementation of above approach
using System;
 
class GFG
{
// Function to return minimum
// number of insertions required
static int minInsertions(int[] H,
                         int n, int K)
{
    // Initialize insertions to 0
    int inser = 0;
 
    for (int i = 1; i < n; ++i)
    {
        float diff = Math.Abs(H[i] - H[i - 1]);
 
        if (diff <= K)
            continue;
        else
            inser += (int)Math.Ceiling(diff / K) - 1;
    }
 
    // return total insertions
    return inser;
}
 
// Driver Code
static void Main()
{
    int[] H = new int[]{ 2, 4, 8, 16 };
    int K = 3;
    int n = H.Length;
    Console.WriteLine(minInsertions(H, n, K));
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
3

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