📌  相关文章
📜  通过添加 1、2 或 5 来最小化使所有数组元素相同所需的步骤

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

给定一个大小为N的数组arr[] ,任务是计算使所有数组元素相同所需的最少步骤数,方法是将1、25 添加到数组的每个元素的(N – 1) 个元素上。步。

例子:

朴素的方法:最简单的方法是递归地尝试所有可能的组合,将数字1、2 和 5相加使所有元素变得相同,并计算所有此类组合所需的步骤数。最后,将它们中的最小值打印为所需答案。

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

高效方法:上述方法可以通过以下观察进行优化:

  • 将数字K添加到除一个之外的所有索引(例如索引X )与从索引X处的值中删除K相同。
  • 这将搜索最终数组元素的界限降低到小于等于给定数组中存在的最小值。
  • 设最小值为A ,则优化操作后的最终值可以是AA – 1A – 2
  • 计算中不考虑A – 3等的原因是因为A + 1需要 2 步才能到达 (-1, -2 ), A + 2需要一步才能到达A – 3 (- 5) 但可以只需一步即可轻松到达A ( A+1需要 1 步(-1)才能到达A,A + 2需要 1 步(-2)才能到达 A)。
  • 此外, A + 3需要 2 步 (-5, -1) 才能到达A – 32步才能再次到达A (-1, -2)。
  • 因此,不需要考虑A-3或任何更低的碱基。

因此,我们的想法是通过减去1、2 和 5来找到将所有数组元素减少到它们的最小元素(例如minE )、 minE – 1minE – 2所需的操作计数。打印上述三个操作中的最小值。

下面是上述方法的实现:

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


Javascript


输出:
3

时间复杂度: O(N),其中 N 是给定数组的大小。
辅助空间: O(1)

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