📌  相关文章
📜  插入 MEX 和 Array Max K 次平均值后的唯一元素计数

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

插入 MEX 和 Array Max K 次平均值后的唯一元素计数

给定一个包含N个非负整数的数组A[]和一个整数K 。每次可以做以下两个操作

  • 查找数组的 MAX 和 MEX(向上舍入到最接近的更大整数)的平均值。
  • 在数组中插入计算的平均值。

执行上述操作K次后,找出数组中存在的唯一元素的计数。

注意: MEX 是数组中不存在的最小正整数。

例子:

朴素的方法:遍历给定的数组 K 次,并且在每次迭代中:

  • 找出数组中的 MAX 和 MEX。
  • 计算平均值。
  • 将该元素插入到数组中。

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

高效的方法:问题的解决方法基于以下两种情况:

请按照以下步骤解决问题:

  • 创建一个哈希数组,用于存储唯一元素。
  • 将所有给定的数组元素推入哈希数组。
  • 计算给定数组的 Max 和 MEX。
    • 如果Max 大于 MEX ,则计算平均值并推入哈希数组。
    • 如果MEX 大于 Max ,只需将K添加到数组中唯一元素的计数中,因为在所有 K 操作中,唯一元素都会添加到数组中。
  • 返回哈希数组中唯一元素的计数。

下面是上述方法的实现。

C++
//c++ program for Count number Unique element after
//adding average of MEX and MAX in array K times.
#include 
using namespace std;
 
int uniqueElement(vector& A, int K)
{
    // Hash array
    unordered_map mp;
 
    int max_no = 0;
    // Find out MAX of given Array
    for (int i = 0; i < A.size(); i++) {
        mp[A[i]]++;
        max_no = max(max_no, A[i]);
    }
 
    int mex = INT_MIN;
   
    // Find out MEX of given Array
    for (int i = 0; i < max_no; i++) {
        if (mp.find(i) == mp.end()) {
            mex = i;
            break;
        }
    }
    if (mex == INT_MIN)
        mex = max_no + 1;
 
    // Hash array contains only unique elements
    // So number of unique elements in array =
    // size of Hash array
    int unique = mp.size();
 
    if (K != 0) {
        if (max_no > mex) {
 
      // Calculated rounded average of MAX and MEX
            int avg = round((float)(max_no + mex) / 2);
 
        // If MAX > MEX and avg in not present
        //  in array Increment count of unique
        //element by one.
            if (mp[avg] == 0)
                unique++;
        }
 
        // If MEX > MAX, for every operation, one
        //  new unique element is added in array
        else {
            unique += K;
        }
    }
    return unique;
}
//Driver code
int main()
{
    vector A = { 3, 0, 2, 4, 1, 2, 3, 5 };
    int K = 3;
 
    cout << uniqueElement(A, K);
 
    return 0;
}


Python3
# python3 program for Count number Unique element after
# adding average of MEX and MAX in array K times.
INT_MIN = -2147483647 - 1
 
def uniqueElement(A, K):
 
    # Hash array
    mp = {}
    max_no = 0
     
    # Find out MAX of given Array
    for i in range(0, len(A)):
        mp[A[i]] = mp[A[i]] + 1 if A[i] in mp else 1
        max_no = max(max_no, A[i])
 
    mex = INT_MIN
 
    # Find out MEX of given Array
    for i in range(0, max_no):
        if (not i in mp):
            mex = i
            break
 
    if (mex == INT_MIN):
        mex = max_no + 1
 
    # Hash array contains only unique elements
    # So number of unique elements in array =
    # size of Hash array
    unique = len(mp)
 
    if (K != 0):
        if (max_no > mex):
 
            # Calculated rounded average of MAX and MEX
            avg = round((max_no + mex) / 2)
 
        # If MAX > MEX and avg in not present
        # in array Increment count of unique
        # element by one.
            if (mp[avg] == 0):
                unique += 1
 
        # If MEX > MAX, for every operation, one
        # new unique element is added in array
        else:
            unique += K
 
    return unique
 
# Driver code
if __name__ == "__main__":
 
    A = [3, 0, 2, 4, 1, 2, 3, 5]
    K = 3
 
    print(uniqueElement(A, K))
 
    # This code is contributed by rakeshsahni


C#
// c# program for Count number Unique element after
// adding average of MEX and MAX in array K times.
using System;
using System.Collections.Generic;
 
class GFG {
 
  static int uniqueElement(int[] A, int K)
  {
 
    // Hash array
    Dictionary mp
      = new Dictionary();
 
    int max_no = 0;
 
    // Find out MAX of given Array
    for (int i = 0; i < A.Length; i++) {
      if (mp.ContainsKey(A[i])) {
        mp[A[i]] = mp[A[i]] + 1;
      }
      else {
        mp.Add(A[i], 1);
      }
      max_no = Math.Max(max_no, A[i]);
    }
 
    int mex = Int32.MinValue;
 
    // Find out MEX of given Array
    for (int i = 0; i < max_no; i++) {
      if (!mp.ContainsKey(i)) {
        mex = i;
        break;
      }
    }
    if (mex == Int32.MinValue)
      mex = max_no + 1;
 
    // Hash array contains only unique elements
    // So number of unique elements in array =
    // size of Hash array
    int unique = mp.Count;
 
    if (K != 0) {
      if (max_no > mex) {
 
        // Calculated rounded average of MAX and MEX
        float temp = (max_no + mex) / 2;
        int avg = (int)Math.Round(temp);
 
        // If MAX > MEX and avg in not present
        //  in array Increment count of unique
        // element by one.
        if (mp[avg] == 0)
          unique++;
      }
 
      // If MEX > MAX, for every operation, one
      //  new unique element is added in array
      else {
        unique += K;
      }
    }
    return unique;
  }
 
  // Driver code
  public static void Main()
  {
    int[] A = { 3, 0, 2, 4, 1, 2, 3, 5 };
    int K = 3;
 
    Console.Write(uniqueElement(A, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出:

9

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