📌  相关文章
📜  最小化所需的插入,以使所有相邻阵列元素对的最大和最小值之比最大为K

📅  最后修改于: 2021-04-17 16:57:15             🧑  作者: Mango

给定一个数组arr []和一个整数K ( > 1 ),任务是找到所需的最小插入数目,以使数组中所有相邻元素对的最大和最小比率减小到最多K。

例子:

方法:这个想法是贪婪地使用。请按照以下步骤解决问题:

  • 遍历数组arr []
  • 初始化一个变量,例如ans ,以存储所需的插入次数。
  • 遍历范围[0,N – 2]并执行以下步骤:
    • 计算min(arr [i],arr [i + 1])max(arr [i],arr [i + 1])并将其存储在变量ab中
    • 如果(b> K * a):更新a = K * a并将ans增加1
  • 打印ans的值作为所需答案。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the minimum
// number of insertions required
int mininsert(int arr[], int K, int N)
{
    // Stores the number of
    // insertions required
    int ans = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
 
        // Store the minimum array element
        int a = min(arr[i], arr[i + 1]);
 
        // Store the maximum array element
        int b = max(arr[i], arr[i + 1]);
 
        // Checking condition
        while (K * a < b) {
 
            a *= K;
 
            // Increase current count
            ans++;
        }
    }
 
    // Return the count of insertions
    return ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 10, 25, 21 };
    int K = 2;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << mininsert(arr, K, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to count the minimum
// number of insertions required
static int mininsert(int arr[], int K, int N)
{
   
    // Stores the number of
    // insertions required
    int ans = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
 
        // Store the minimum array element
        int a = Math.min(arr[i], arr[i + 1]);
 
        // Store the maximum array element
        int b = Math.max(arr[i], arr[i + 1]);
 
        // Checking condition
        while (K * a < b) {
 
            a *= K;
 
            // Increase current count
            ans++;
        }
    }
 
    // Return the count of insertions
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 2, 10, 25, 21 };
    int K = 2;
    int N = arr.length;
 
    System.out.print(mininsert(arr, K, N));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to count the minimum
# number of insertions required
def mininsert(arr, K, N) :
     
    # Stores the number of
    # insertions required
    ans = 0
 
    # Traverse the array
    for i in range(N - 1):
 
        # Store the minimum array element
        a = min(arr[i], arr[i + 1])
 
        # Store the maximum array element
        b = max(arr[i], arr[i + 1])
 
        # Checking condition
        while (K * a < b) :
            a *= K
 
            # Increase current count
            ans += 1
         
    # Return the count of insertions
    return ans
 
# Driver Code
arr = [ 2, 10, 25, 21 ]
K = 2
N = len(arr)
 
print(mininsert(arr, K, N))
 
# This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to count the minimum
  // number of insertions required
  static int mininsert(int[] arr, int K, int N)
  {
 
    // Stores the number of
    // insertions required
    int ans = 0;
 
    // Traverse the array
    for (int i = 0; i < N - 1; i++) {
 
      // Store the minimum array element
      int a = Math.Min(arr[i], arr[i + 1]);
 
      // Store the maximum array element
      int b = Math.Max(arr[i], arr[i + 1]);
 
      // Checking condition
      while (K * a < b) {
 
        a *= K;
 
        // Increase current count
        ans++;
      }
    }
 
    // Return the count of insertions
    return ans;
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int[] arr = { 2, 10, 25, 21 };
    int K = 2;
    int N = arr.Length;
 
    Console.Write(mininsert(arr, K, N));
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
3

时间复杂度: O(N * log(M)),其中M数组中最大的元素。
辅助空间: O(1)