插入 MEX 和 Array Max K 次平均值后的唯一元素计数
给定一个包含N个非负整数的数组A[]和一个整数K 。每次可以做以下两个操作
- 查找数组的 MAX 和 MEX(向上舍入到最接近的更大整数)的平均值。
- 在数组中插入计算的平均值。
执行上述操作K次后,找出数组中存在的唯一元素的计数。
注意: MEX 是数组中不存在的最小正整数。
例子:
Input: A = [ 0, 5, 1, 2, 1, 8 ], K=1
Output: 6
Explanation: In first operation, Max = 8 and MEX = 3.
So average is ( 8 + 3 ) / 2 = 5.5 = 6 (rounded up).
Insert 6 in the array, then Array becomes: [ 0, 5, 1, 2, 1, 8, 6 ].
So, Count of unique element is 6.
Input: A = [ 0, 1, 2 ], K = 2
Output: 5
Explanation: In first operation, Max = 2 and MEX = 3.
So average is ( 2 + 3 ) / 2 = 2.5 = 3 (rounded up).
Add 3 in the array, then Array becomes: [ 0, 1, 2, 3 ].
In Second Operation, Again Max = 3 and MEX = 4, Average = 4.
So Add 4 in the array. Now the Array becomes [ 0, 1, 2, 3, 4 ].
So, Count of unique element is 5.
朴素的方法:遍历给定的数组 K 次,并且在每次迭代中:
- 找出数组中的 MAX 和 MEX。
- 计算平均值。
- 将该元素插入到数组中。
时间复杂度: O(N*K)
辅助空间: O(1)
高效的方法:问题的解决方法基于以下两种情况:
Case-1 (When Max > MEX): The average of Max and MEX will always lie between Max and MEX and there will be no changes of Max value or MEX value.
So it does not matter if the average is added once or K times.
If average is unique then unique element count will increase by 1, otherwise, the unique count will be the same.
Case-2 (When Max < MEX): The average of Max and MEX will always be greater than the existing Max. So at every step a new unique element will be added to the array, i.e. total K elements added in K operations.
e.g. arr[] = {0, 1, 2} and K = 2.
- At first step Max and MEX are 2 and 3 respectively. So (2+3)/2 = 3 will be added. The array will be {0, 1, 2, 3}.
- At 2nd step Max and MEX are 3 and 4 respectively. So (3+4)/2 = 4 will be added. The array will be {0, 1, 2, 3, 4}. Therefore 2 unique elements will be added in 2 operation.
请按照以下步骤解决问题:
- 创建一个哈希数组,用于存储唯一元素。
- 将所有给定的数组元素推入哈希数组。
- 计算给定数组的 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)
辅助空间: 在 )