📌  相关文章
📜  在 Array 元素中进行 K 次替换后最大化元素的出现次数

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

在 Array 元素中进行 K 次替换后最大化元素的出现次数

给定一个具有N个整数的数组arr[]和一个整数K ,任务是找到一个数组,使得它在数组元素内进行K次替换后最多包含单个元素。

例子:

方法:解决这个问题的方法是基于以下思想:

以下是实现上述方法的算法:

  • 创建一个哈希图来存储每个不同元素的频率。
  • 查找数组中出现的最大元素。
  • 然后分配最大出现元素的每个元素值。
  • 确保在分配元素之前和分配元素之后注意频率。
  • 为此,首先降低当前元素的频率,并在为其分配最大出现值后,增加其在哈希图中的值。

下面是上述方法的实现:

C++
// C++ code for the above discussed approach
#include 
using namespace std;
 
// Function to maximizeTheElements
vector maximizeTheElements(
    int N, vector arr, int K)
{
 
    // Map tp store the frequency
    // of the elements
    unordered_map mp;
 
    int max_freq = 0, max_element = -1;
    for (auto x : arr) {
        mp[x]++;
 
        // Getting max_element
        if (mp[x] > max_freq) {
            max_freq = mp[x];
            max_element = x;
        }
    }
 
    for (int i = 0; i < N && K > 0; i++) {
 
        // If the element is not equal
        // to the max_element
        if (arr[i] != max_element) {
 
            // Decrease its frequency from the map
            mp[arr[i]] -= 1;
 
            // Assign the max frequency element
            arr[i] = max_element;
 
            // Increase its frequency in the map
            mp[arr[i]] += 1;
 
            // Decrease the operation by 1
            K -= 1;
        }
    }
 
    // Return the modified array
    return arr;
}
 
// Driver Function
int main()
{
 
    int N = 7;
    vector arr = { 1, 2, 1, 5, 1, 6, 7 };
    int K = 3;
 
    // Function call
    vector res = maximizeTheElements(N, arr, K);
 
    for (auto x : res) {
        cout << x << " ";
    }
    cout << endl;
 
    return 0;
}


Java
// Java code for the above discussed approach
import java.util.*;
 
class GFG {
 
  // Function to maximizeTheElements
  static int[] maximizeTheElements(
    int N, int arr[], int K)
  {
 
    // Map tp store the frequency
    // of the elements
    HashMap m = new HashMap();
    int max_freq = 0, max_element = -1;
    for(int i = 0; i  < arr.length; i++){
      if(m.containsKey(arr[i])){
        m.put(arr[i], m.get(arr[i]) + 1);
      }
      else{
        m.put(arr[i],1);
      }
 
      // Getting max_element
      if (m.get(arr[i]) > max_freq) {
        max_freq = m.get(arr[i]);
        max_element = arr[i];
      }
    }
 
    for (int i = 0; i < N; i++) {
      if(K <= 0)break;
      // If the element is not equal
      // to the max_element
      if (arr[i] != max_element) {
 
        // Decrease its frequency from the map
        m.put(arr[i], m.get(arr[i]) - 1);
 
        // Assign the max frequency element
        arr[i] = max_element;
 
        // Increase its frequency in the map
        m.put(arr[i], m.get(arr[i]) + 1);
 
        // Decrease the operation by 1
        K -= 1;
      }
    }
 
    // Return the modified array
    return arr;
  }
 
  // Driver Function
  public static void main (String[] args) {
    int N = 7;
    int  arr[] = { 1, 2, 1, 5, 1, 6, 7 };
    int K = 3;
 
    // Function call
    int res[] = maximizeTheElements(N, arr, K);
 
    for (int x : res) {
      System.out.print(x + " ");
    }
 
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 code for the above discussed approach
 
# Function to maximizeTheElements
def maximizeTheElements(N, arr, K):
 
        # Map tp store the frequency
        # of the elements
    mp = {}
 
    max_freq, max_element = 0, -1
    for x in arr:
        mp[x] = mp[x] + 1 if x in mp else 1
 
        # Getting max_element
        if (mp[x] > max_freq):
            max_freq = mp[x]
            max_element = x
 
    for i in range(0, N):
        if K <= 0:
            break
            # If the element is not equal
            # to the max_element
        if (arr[i] != max_element):
 
                        # Decrease its frequency from the map
            mp[arr[i]] -= 1
 
            # Assign the max frequency element
            arr[i] = max_element
 
            # Increase its frequency in the map
            mp[arr[i]] += 1
 
            # Decrease the operation by 1
            K -= 1
 
        # Return the modified array
    return arr
 
# Driver Function
if __name__ == "__main__":
 
    N = 7
    arr = [1, 2, 1, 5, 1, 6, 7]
    K = 3
 
    # Function call
    res = maximizeTheElements(N, arr, K)
 
    for x in res:
        print(x, end=" ")
 
    # This code is contributed by rakeshsahni


Javascript


输出
1 1 1 1 1 1 7 

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