📌  相关文章
📜  使前 N 个自然数的排列相等所需的最小运算次数

📅  最后修改于: 2021-10-25 06:49:15             🧑  作者: Mango

给定大小为N的数组A[] ,其中包含前N 个自然数和整数K的排列,任务是通过选择K ( 1 < K ≤ N ) 找到使所有数组元素相等所需的最小操作次数连续的数组元素并将它们替换为所选元素中的最小值。

例子:

方法:这个想法基于以下观察:

  • 排列无关紧要。这与在开始时放置最小值相同。
  • 可以通过从最小索引开始并向前移动K来计算所需的最佳操作次数。

这个问题可以通过想象最小值在数组的开头并从那里开始,选择K个连续元素来解决。请按照以下步骤解决问题:

  • 初始化变量icount0 ,分别用于迭代和计算最小操作次数。
  • i小于N – 1时循环,因为当i达到N – 1 时,所有元素都已相等。在每次当前迭代中,执行以下步骤:
    • 计数增加1
    • i增加K-1,因为最右边的更新元素将再次用于下一段。
  • 打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// number of operations required
// to make all array elements equal
int MinimumOperations(int A[],
                      int N, int K)
{
    // Store the count of
    // operations required
    int Count = 0;
 
    int i = 0;
 
    while (i < N - 1) {
 
        // Increment by K - 1, as the last
        // element will be used again for
        // the next K consecutive elements
        i = i + K - 1;
 
        // Increment count by 1
        Count++;
    }
 
    // Return the result
    return Count;
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 5, 4, 3, 1, 2 };
    int K = 3;
    int N = sizeof(A) / sizeof(A[0]);
 
    cout << MinimumOperations(A, N, K) << endl;
 
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
       
      // Function to find the minimum
    // number of operations required
    // to make all array elements equal
 
    static int MinimumOperations(int[] A, int N, int K)
    {
        // Store the count of
        // operations required
        int Count = 0;
 
        int i = 0;
 
        while (i < N - 1) {
 
            // Increment by K - 1, as the last
            // element will be used again for
            // the next K consecutive elements
            i = i + K - 1;
 
            // Increment count by 1
            Count++;
        }
 
        // Return the result
        return Count;
    }
 
    // Driver Code
    public static void main (String[] args) {
        // Given Input
        int[] A = { 5, 4, 3, 1, 2 };
        int K = 3;
        int N = A.length;
 
        System.out.println(MinimumOperations(A, N, K));
    }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
 
# Function to find the minimum
# number of operations required
# to make all array elements equal
def MinimumOperations(A, N, K):
     
    # Store the count of
    # operations required
    Count = 0
 
    i = 0
 
    while (i < N - 1):
 
        # Increment by K - 1, as the last
        # element will be used again for
        # the next K consecutive elements
        i = i + K - 1
 
        # Increment count by 1
        Count += 1
 
    # Return the result
    return Count
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 5, 4, 3, 1, 2 ]
    K = 3
    N = len(A)
 
    print (MinimumOperations(A, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG {
    // Function to find the minimum
    // number of operations required
    // to make all array elements equal
 
    static int MinimumOperations(int[] A, int N, int K)
    {
        // Store the count of
        // operations required
        int Count = 0;
 
        int i = 0;
 
        while (i < N - 1) {
 
            // Increment by K - 1, as the last
            // element will be used again for
            // the next K consecutive elements
            i = i + K - 1;
 
            // Increment count by 1
            Count++;
        }
 
        // Return the result
        return Count;
    }
 
    // Driver Code
    public static void Main()
    {
        // Given Input
        int[] A = { 5, 4, 3, 1, 2 };
        int K = 3;
        int N = A.Length;
 
        Console.WriteLine(MinimumOperations(A, N, K));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
2

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