给定一个数组arr [] 和一个整数K ( > 1 ),任务是找到所需的最小插入次数,使得数组中所有相邻元素对的最大值和最小值之比减少到至多K 。
例子:
Input : arr[] = { 2, 10, 25, 21 }, K = 2
Output: 3
Explanation:
Following insertions makes the array satisfy the required conditions {2, 4, 7, 10, 17, 25, 21}.
Input : arr[] = {2, 4, 1}, K = 2
Output: 1
做法:思路是贪婪地使用。请按照以下步骤解决问题:
- 遍历数组arr[] 。
- 初始化一个变量,比如ans ,以存储所需的插入次数。
- 迭代范围[0, N – 2]并执行以下步骤:
- 计算min(arr[i], arr[i + 1])和max(arr[i], arr[i + 1])并将其存储在变量中,例如a和b 。
- 如果 (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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live