给定一个大小为N的数组arr [] ,任务是通过在每一步的数组的正好(N – 1)个元素上加上1、2或5 ,以使所有数组元素相同所需的最小步骤数。 。
例子:
Input: N = 4, arr[] = {2, 2, 3, 7}
Output: 2
Explanation:
Step 1: {2, 2, 3, 7} -> {3, 3, 3, 8}
Step 2: {3, 3, 3, 8} -> {8, 8, 8, 8}
Input: N = 3, arr[] = {10, 7, 12}
Output: 3
天真的方法:最简单的方法是递归地尝试所有可能的组合,将数字1、2和5相加,以使所有元素都相同,并计算所有此类组合所需的步骤数。最后,将其中的最小数量打印为所需答案。
时间复杂度: O(M M ),其中M是数组中存在的最大元素。
辅助空间: O(1)
高效的方法:可以通过以下观察来优化上述方法:
- 向除一个索引(例如索引X )以外的所有索引加数字K的方法与从索引X的值中删除K相同。
- 这将搜索最终数组元素的界限减小到小于等于给定数组中存在的最小值的范围。
- 设最小值为A ,则最优操作后的最终值可以为A , A-1或A-2 。
- 在计算中未考虑A – 3之类的原因是因为A +1需要2步才能到达那里(-1,-2), A + 2需要1步才能到达A – 3 (-5)但可以只需一个步骤即可轻松达到A要求( A + 1需要1步(-1)才能达到A,而A + 2需要1步(-2)才能达到A)。
- 同样, A + 3需要2步(-5,-1)才能到达A – 3,并需要2步才能再次到达A (-1,-2)。
- 因此,不需要考虑A – 3或任何较低的底数。
因此,我们的想法是找到将所有数组元素减为最小元素(例如minE ), minE – 1和minE – 2所需的操作计数,方法是减去1、2和5 。打印以上三个操作中的最小值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate the minimum
// number of steps
int calculate_steps(int arr[], int n,
int minimum)
{
// count stores number of operations
// required to make all elements
// equal to minimum value
int count = 0;
// Remark, the array should remain
// unchanged for further calculations
// with different minumum
for (int i = 0; i < n; i++) {
// Storing the current value of
// arr[i] in val
int val = arr[i];
if (arr[i] > minimum) {
// Finds how much extra amount
// is to be removed
arr[i] = arr[i] - minimum;
// Subtract the maximum number
// of 5 and stores remaining
count += arr[i] / 5;
arr[i] = arr[i] % 5;
// Subtract the maximum number
// of 2 and stores remaining
count += arr[i] / 2;
arr[i] = arr[i] % 2;
if (arr[i]) {
count++;
}
}
// Restores the actual value
// of arr[i]
arr[i] = val;
}
// Return the count
return count;
}
// Function to find the minimum number
// of steps to make array elements same
int solve(int arr[], int n)
{
// Sort the array in descending order
sort(arr, arr + n, greater());
// Stores the minimum array element
int minimum = arr[n - 1];
int count1 = 0, count2 = 0, count3 = 0;
// Stores the operations required
// to make array elements equal to minimum
count1 = calculate_steps(arr, n, minimum);
// Stores the operations required
// to make array elements equal to minimum - 1
count2 = calculate_steps(arr, n, minimum - 1);
// Stores the operations required
// to make array elements equal to minimum - 2
count3 = calculate_steps(arr, n, minimum - 2);
// Return minimum of the three counts
return min(count1, min(count2, count3));
}
// Driver Code
int main()
{
int arr[] = { 3, 6, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << solve(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to calculate the minimum
// number of steps
static int calculate_steps(Integer arr[],
int n, int minimum)
{
// count stores number of operations
// required to make all elements
// equal to minimum value
int count = 0;
// Remark, the array should remain
// unchanged for further calculations
// with different minumum
for (int i = 0; i < n; i++)
{
// Storing the current value of
// arr[i] in val
int val = arr[i];
if (arr[i] > minimum)
{
// Finds how much extra amount
// is to be removed
arr[i] = arr[i] - minimum;
// Subtract the maximum number
// of 5 and stores remaining
count += arr[i] / 5;
arr[i] = arr[i] % 5;
// Subtract the maximum number
// of 2 and stores remaining
count += arr[i] / 2;
arr[i] = arr[i] % 2;
if (arr[i] > 0)
{
count++;
}
}
// Restores the actual value
// of arr[i]
arr[i] = val;
}
// Return the count
return count;
}
// Function to find the minimum number
// of steps to make array elements same
static int solve(Integer arr[], int n)
{
// Sort the array in descending order
Arrays.sort(arr, Collections.reverseOrder());
// Stores the minimum array element
int minimum = arr[n - 1];
int count1 = 0, count2 = 0, count3 = 0;
// Stores the operations required
// to make array elements equal
// to minimum
count1 = calculate_steps(arr, n,
minimum);
// Stores the operations required
// to make array elements equal to
// minimum - 1
count2 = calculate_steps(arr, n,
minimum - 1);
// Stores the operations required
// to make array elements equal to
// minimum - 2
count3 = calculate_steps(arr, n,
minimum - 2);
// Return minimum of the three counts
return Math.min(count1, Math.min(count2,
count3));
}
// Driver Code
public static void main(String[] args)
{
Integer arr[] = {3, 6, 6};
int N = arr.length;
System.out.print(solve(arr, N));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program for the above approach
# Function to calculate the minimum
# number of steps
def calculate_steps(arr, n, minimum):
# count stores number of operations
# required to make all elements
# equal to minimum value
count = 0
# Remark, the array should remain
# unchanged for further calculations
# with different minumum
for i in range(n):
# Storing the current value of
# arr[i] in val
val = arr[i]
if (arr[i] > minimum):
# Finds how much extra amount
# is to be removed
arr[i] = arr[i] - minimum
# Subtract the maximum number
# of 5 and stores remaining
count += arr[i] // 5
arr[i] = arr[i] % 5
# Subtract the maximum number
# of 2 and stores remaining
count += arr[i] // 2
arr[i] = arr[i] % 2
if (arr[i]):
count += 1
# Restores the actual value
# of arr[i]
arr[i] = val
# Return the count
return count
# Function to find the minimum number
# of steps to make array elements same
def solve(arr, n):
# Sort the array in descending order
arr = sorted(arr)
arr = arr[::-1]
# Stores the minimum array element
minimum = arr[n - 1]
count1 = 0
count2 = 0
count3 = 0
# Stores the operations required
# to make array elements equal to minimum
count1 = calculate_steps(arr, n, minimum)
# Stores the operations required
# to make array elements equal to minimum - 1
count2 = calculate_steps(arr, n, minimum - 1)
# Stores the operations required
# to make array elements equal to minimum - 2
count3 = calculate_steps(arr, n, minimum - 2)
# Return minimum of the three counts
return min(count1, min(count2, count3))
# Driver Code
if __name__ == '__main__':
arr = [ 3, 6, 6 ]
N = len(arr)
print(solve(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
public class GFG{
// Function to calculate the minimum
// number of steps
static int calculate_steps(int []arr, int n,
int minimum)
{
// count stores number of operations
// required to make all elements
// equal to minimum value
int count = 0;
// Remark, the array should remain
// unchanged for further calculations
// with different minumum
for (int i = 0; i < n; i++) {
// Storing the current value of
// arr[i] in val
int val = arr[i];
if (arr[i] > minimum) {
// Finds how much extra amount
// is to be removed
arr[i] = arr[i] - minimum;
// Subtract the maximum number
// of 5 and stores remaining
count += arr[i] / 5;
arr[i] = arr[i] % 5;
// Subtract the maximum number
// of 2 and stores remaining
count += arr[i] / 2;
arr[i] = arr[i] % 2;
if (arr[i]>0) {
count++;
}
}
// Restores the actual value
// of arr[i]
arr[i] = val;
}
// Return the count
return count;
}
// Function to find the minimum number
// of steps to make array elements same
static int solve(int []arr, int n)
{
// Sort the array in descending order
Array.Sort(arr);
Array.Reverse(arr);
// Stores the minimum array element
int minimum = arr[n - 1];
int count1 = 0, count2 = 0, count3 = 0;
// Stores the operations required
// to make array elements equal to minimum
count1 = calculate_steps(arr, n, minimum);
// Stores the operations required
// to make array elements equal to minimum - 1
count2 = calculate_steps(arr, n, minimum - 1);
// Stores the operations required
// to make array elements equal to minimum - 2
count3 = calculate_steps(arr, n, minimum - 2);
// Return minimum of the three counts
return Math.Min(count1, Math.Min(count2, count3));
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 3, 6, 6 };
int N = arr.Length;
Console.Write(solve(arr, N));
}
}
// This code contributed by Rajput-Ji
输出:
3
时间复杂度: O(N),其中N是给定数组的大小。
辅助空间: O(1)