📌  相关文章
📜  通过重复递增子序列使所有数组元素等于 K

📅  最后修改于: 2021-10-27 06:58:12             🧑  作者: Mango

给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是通过将子序列的所有元素重复递增1来使所有数组元素等于K
注意: K的值至少是数组最大元素

例子:

方法:这个想法是使用哈希来跟踪子序列中的元素。当子序列中的元素增加1 时,其频率减少1 ,其修改值的频率增加1
请按照以下步骤解决问题:

  • 初始化一个变量,比如ans ,它存储所需的最少操作数。
  • 初始化一个 Hashmap,比如mp ,并存储数组元素的频率。
  • K的频率小于N 时,即mp[K] < N ,执行以下操作:
    • 使用变量i遍历范围[1, K – 1]
      • 如果mp[i]大于0 ,则降低当前值的频率并将下一组(i + 1)元素的频率增加1
      • 如果(i + 1)不是任何先前值的一部分,则跳过它并继续遍历循环。
    • ans的值增加 1。
  • 完成以上步骤后,打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of operations required to make all
// elements equal to k
void minOperations(int arr[], int n, int k)
{
    // Initialize a hashmap
    map mp;
 
    // Store frequency of array elements
    for (int i = 0; i < n; i++) {
        mp[arr[i]]++;
    }
 
    // Store the minimum number of
    // operations required
    int ans = 0;
 
    // Iterate until all array elements
    // becomes equal to K
    while (mp[k] < n) {
 
        // Iterate through range [1, k - 1]
        // since only one element can be
        // increased from each group
        for (int i = 1; i <= k - 1; i++) {
 
            // Check if the current number
            // has frequency > 0, i.e.,
            // it is a part of a group
            if (mp[i]) {
 
                // If true, decrease the
                // frequency of current
                // group of element by 1
                mp[i]--;
 
                // Increase the frequency
                // of the next group of
                // elements by 1
                mp[i + 1]++;
 
                // If the next element is
                // not the part of any
                // group, then skip it
                if (mp[i + 1] == 1) {
                    i++;
                }
            }
        }
 
        // Increment count of operations
        ans++;
    }
 
    // Print the count of operations
    cout << ans;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 3, 4 };
    int K = 5;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    minOperations(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to find the minimum number
  // of operations required to make all
  // elements equal to k
  static void minOperations(int arr[], int n, int k)
  {
 
    // Initialize a hashmap
    Map mp = new HashMap<>();
 
    // Store frequency of array elements
    for (int i = 0; i < n; i++)
    {
      if (mp.containsKey(arr[i]))
      {
        mp.put(arr[i], mp.get(arr[i]) + 1);
      }
      else
      {
        mp.put(arr[i], 1);
      }
    }
 
    // Store the minimum number of
    // operations required
    int ans = 0;
 
    // Iterate until all array elements
    // becomes equal to K
    while (mp.containsKey(k) == false
           || mp.get(k) < n) {
 
      // Iterate through range [1, k - 1]
      // since only one element can be
      // increased from each group
      for (int i = 1; i <= k - 1; i++) {
 
        // Check if the current number
        // has frequency > 0, i.e.,
        // it is a part of a group
        if (mp.containsKey(i) && mp.get(i) > 0) {
 
          // If true, decrease the
          // frequency of current
          // group of element by 1
          mp.put(i, mp.get(i) - 1);
 
          // Increase the frequency
          // of the next group of
          // elements by 1
          if (mp.containsKey(i + 1))
            mp.put(i + 1, mp.get(i + 1) + 1);
          else
            mp.put(i + 1, 1);
 
          // If the next element is
          // not the part of any
          // group, then skip it
          if (mp.containsKey(i + 1)
              && mp.get(i + 1) == 1) {
            i++;
          }
        }
      }
 
      // Increment count of operations
      ans++;
    }
 
    // Print the count of operations
    System.out.print(ans);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { 2, 3, 3, 4 };
    int K = 5;
    int N = arr.length;
 
    // Function Call
    minOperations(arr, N, K);
  }
}
 
// This code is contributed by Dharanendra L V.


C#
// C# program for the above approach
 
using System;
using System.Collections.Generic;
 
public class GFG
{
 
  // Function to find the minimum number
  // of operations required to make all
  // elements equal to k
  static void minOperations(int []arr, int n, int k)
  {
 
    // Initialize a hashmap
    Dictionary mp = new Dictionary();
 
    // Store frequency of array elements
    for (int i = 0; i < n; i++)
    {
      if (mp.ContainsKey(arr[i]))
      {
        mp[arr[i]] =  mp[arr[i]] + 1;
      }
      else
      {
        mp.Add(arr[i], 1);
      }
    }
 
    // Store the minimum number of
    // operations required
    int ans = 0;
 
    // Iterate until all array elements
    // becomes equal to K
    while (mp.ContainsKey(k) == false
           || mp[k] < n) {
 
      // Iterate through range [1, k - 1]
      // since only one element can be
      // increased from each group
      for (int i = 1; i <= k - 1; i++) {
 
        // Check if the current number
        // has frequency > 0, i.e.,
        // it is a part of a group
        if (mp.ContainsKey(i) && mp[i] > 0) {
 
          // If true, decrease the
          // frequency of current
          // group of element by 1
          mp[i] = mp[i] - 1;
 
          // Increase the frequency
          // of the next group of
          // elements by 1
          if (mp.ContainsKey(i + 1))
            mp[i + 1] = mp[i + 1] + 1;
          else
            mp.Add(i + 1, 1);
 
          // If the next element is
          // not the part of any
          // group, then skip it
          if (mp.ContainsKey(i + 1)
              && mp[i + 1] == 1) {
            i++;
          }
        }
      }
 
      // Increment count of operations
      ans++;
    }
 
    // Print the count of operations
    Console.Write(ans);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int []arr = { 2, 3, 3, 4 };
    int K = 5;
    int N = arr.Length;
 
    // Function Call
    minOperations(arr, N, K);
  }
}
 
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程