📌  相关文章
📜  最小化操作次数以使所有元素在给定条件下相等

📅  最后修改于: 2022-05-13 01:56:07.502000             🧑  作者: Mango

最小化操作次数以使所有元素在给定条件下相等

给定一个数组arr[]。任务是最小化使arr[]中的所有元素相等所需的操作数。几乎可以用任何其他元素替换arr[]中的任何元素。找到这样做所需的最小操作数,在一个操作中采用arr[] 的任何后缀并将该后缀中的值递增/递减1

例子

方法:这个问题是基于实现的。请按照以下步骤解决给定的问题。

  • 因为,不需要对从 arr 1开始的后缀执行任何操作,因为这可以更改数组中的所有整数。
  • 因此,使arr i等于arr i-1的唯一方法 是从a i开始对 suffix 进行运算, abs(a i −a i-1 )次。
  • 现在,最初更改数组中的值的最佳方法是最小化操作。
  • 为了使arr 1等于arr 2 ,最小操作减少了 abs (arr 2 – arr 1 )
  • 类似地,为了使arr n等于 arr n-1操作 = abs(arr n – arr n-1 )
  • 对于左元素,更改任何元素arr i , 影响abs(a i -a i-1 ) 和 abs(a i+1 -a i )
  • 此外,请注意这一重要事实,即当 a i介于 a i-1和 a i+1之间时,该值最小化。
  • 因此,运算次数从abs(a i -a i-1 )+abs(a i+1 -a i ) 减少到 abs(a i+1 -a i-1 )
  • 返回最终答案。

下面是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
void findMinOperations(vector ar, int n)
{
 
    // Initializing vector to avoid overflows
    vector arr(n + 5);
 
    for (int i = 1; i <= n; i++) {
        arr[i] = ar[i - 1];
    }
    int result = 0;
 
    // Calculating minimum operations to be
    // performed on initial array
    for (int i = 2; i <= n; i++) {
        result += abs(arr[i] - arr[i - 1]);
    }
 
    // Way to change a value to make
    // a1 equal to a2 or a(n)
    // equal to a(n-1)
    int max_operations
        = max(abs(arr[1] - arr[2]),
              abs(arr[n] - arr[n - 1]));
    for (int i = 2; i < n; i++) {
 
        // For the rest of elements
        // taking the max of
        // operations already done +
        // the ones performed here
        max_operations
            = max(
                max_operations,
                abs(arr[i] - arr[i - 1])
                    + abs(arr[i + 1] - arr[i])
                    - abs(arr[i + 1] - arr[i - 1]));
    }
 
    // Print the final result
    cout << result - max_operations << "\n";
}
 
// Driver Code
int main()
{
 
    int N = 3;
    vector arr = { -1, 0, 2 };
    findMinOperations(arr, N);
    return 0;
}


Java
// Java program for above approach
class GFG {
 
  static void findMinOperations(int[] ar, int n) {
 
    // Initializing vector to avoid overflows
    int[] arr = new int[n + 5];
 
    for (int i = 1; i <= n; i++) {
      arr[i] = ar[i - 1];
    }
    int result = 0;
 
    // Calculating minimum operations to be
    // performed on initial array
    for (int i = 2; i <= n; i++) {
      result += Math.abs(arr[i] - arr[i - 1]);
    }
 
    // Way to change a value to make
    // a1 equal to a2 or a(n)
    // equal to a(n-1)
    int max_operations = Math.max(Math.abs(arr[1] - arr[2]),
                                  Math.abs(arr[n] - arr[n - 1]));
    for (int i = 2; i < n; i++) {
 
      // For the rest of elements
      // taking the max of
      // operations already done +
      // the ones performed here
      max_operations = Math.max(
        max_operations,
        Math.abs(arr[i] - arr[i - 1])
        + Math.abs(arr[i + 1] - arr[i])
        - Math.abs(arr[i + 1] - arr[i - 1]));
    }
 
    // Print the final result
    System.out.println(result - max_operations);
  }
 
  // Driver Code
  public static void main(String args[]) {
 
    int N = 3;
    int[] arr = { -1, 0, 2 };
    findMinOperations(arr, N);
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# Python code for the above approach
def findMinOperations(ar, n):
 
    # Initializing vector to avoid overflows
    arr = [0] * (n + 5)
 
    for i in range(1, n + 1):
        arr[i] = ar[i - 1]
 
    result = 0
 
    # Calculating minimum operations to be
    # performed on initial array
    for i in range(2, n + 1):
        result += abs(arr[i] - arr[i - 1])
 
    # Way to change a value to make
    # a1 equal to a2 or a(n)
    # equal to a(n-1)
    max_operations = max(abs(arr[1] - arr[2]), abs(arr[n] - arr[n - 1]))
    for i in range(2, n):
 
        # For the rest of elements
        # taking the max of
        # operations already done +
        # the ones performed here
        max_operations = max(
            max_operations,
            abs(arr[i] - arr[i - 1])
            + abs(arr[i + 1] - arr[i])
            - abs(arr[i + 1] - arr[i - 1]))
 
    # Print the final result
    print((result - max_operations))
 
# Driver Code
N = 3
arr = [-1, 0, 2]
findMinOperations(arr, N)
 
# This code is contributed by gfgking


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
 
  static void findMinOperations(int[] ar, int n) {
 
    // Initializing vector to avoid overflows
    int[] arr = new int[n + 5];
 
    for (int i = 1; i <= n; i++) {
      arr[i] = ar[i - 1];
    }
    int result = 0;
 
    // Calculating minimum operations to be
    // performed on initial array
    for (int i = 2; i <= n; i++) {
      result += Math.Abs(arr[i] - arr[i - 1]);
    }
 
    // Way to change a value to make
    // a1 equal to a2 or a(n)
    // equal to a(n-1)
    int max_operations = Math.Max(Math.Abs(arr[1] - arr[2]),
                                  Math.Abs(arr[n] - arr[n - 1]));
    for (int i = 2; i < n; i++) {
 
      // For the rest of elements
      // taking the max of
      // operations already done +
      // the ones performed here
      max_operations = Math.Max(
        max_operations,
        Math.Abs(arr[i] - arr[i - 1])
        + Math.Abs(arr[i + 1] - arr[i])
        - Math.Abs(arr[i + 1] - arr[i - 1]));
    }
 
    // Print the final result
    Console.Write(result - max_operations);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 3;
    int[] arr = { -1, 0, 2 };
    findMinOperations(arr, N);
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
1

时间复杂度:O(N)
辅助空间:O(1)