📌  相关文章
📜  在最多 K 次更改后最大化 Array 中不同元素的数量

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

在最多 K 次更改后最大化 Array 中不同元素的数量

给定一个数组arr[] ,任务是在最多 K 次更改后找到arr中不同数字的最大数量。在每次更改中,从arr中选择任何元素X并将其更改为Y ,使得L <= Y <= R

例子:

方法:这个问题 可以通过使用无序映射来解决。存储地图中所有元素的频率,然后从L遍历到R ,查看地图中尚不存在的数字,我们可以使用该数字替换arr中的任何重复元素。

  • 首先,存储地图中所有元素的频率。
  • 不同元素的总数将等于地图的大小。
  • 额外元素的数量将等于(数组大小 - 地图大小)
  • 然后从L迭代到R并检查此范围内有多少元素可用于替换数组中存在的额外元素。
  • 将任何额外元素中的每个更改更新为任何其他值的映射。
  • 最后,地图的大小将包含所有不同的元素。
  • 返回地图的大小作为答案。
C++
// C++ program for above approach
#include 
using namespace std;
 
// Function to calculate
// maximum distinct elements possible
// after at most K changes
int maxDistinctElements(int* arr, int K,
                        int L, int R, int n)
{
    // Map to store frequency of all the elements
    unordered_map frequency;
 
    // Count frequency of each element
    for (int x = 0; x < n; x++) {
        frequency[arr[x]] += 1;
    }
 
    // To store number of extra elements
    // that needs to be changed
    int extra = (n - frequency.size());
 
    // Traverse from L to R
    // and see which number is not
    // present in map, use that number
    // to change extra duplicate element
    for (int i = L;
         i <= R and K != 0 and extra != 0;
         i++) {
        if (!frequency[i]) {
            frequency[i] = 1;
            K--;
            extra--;
        }
    }
 
    // Total distinct element will be equal
    // to the size of updated frequency map.
    int ans = frequency.size();
 
    // Return answer
    return ans;
}
 
// Driver Code
int main()
{
    // Test case 1
    int N = 7, L = 1, R = 5, K = 2;
    int arr[7] = { 1, 2, 1, 4, 6, 4, 4 };
    cout << maxDistinctElements(arr, K, L, R, N)
         << endl;
 
    // Test case 2
    K = 1;
    cout << maxDistinctElements(arr, K, L, R, N)
         << endl;
}


Java
// Java program for above approach
import java.util.*;
 
class GFG {
 
    // Function to calculate
    // maximum distinct elements possible
    // after at most K changes
    static int maxDistinctElements(int[] arr, int K, int L, int R, int n)
    {
       
        // Map to store frequency of all the elements
        HashMap frequency = new HashMap();
 
        // Count frequency of each element
        for (int x = 0; x < n; x++) {
            if (frequency.containsKey(arr[x])) {
                frequency.put(arr[x], frequency.get(arr[x]) + 1);
            } else {
                frequency.put(arr[x], 1);
            }
        }
 
        // To store number of extra elements
        // that needs to be changed
        int extra = (n - frequency.size());
 
        // Traverse from L to R
        // and see which number is not
        // present in map, use that number
        // to change extra duplicate element
        for (int i = L; i <= R && K != 0 && extra != 0; i++) {
            if (!frequency.containsKey(i)) {
                frequency.put(i, 1);
                K--;
                extra--;
            }
        }
 
        // Total distinct element will be equal
        // to the size of updated frequency map.
        int ans = frequency.size();
 
        // Return answer
        return ans;
    }
 
    // Driver Code
    public static void main(String[] args) {
        // Test case 1
        int N = 7, L = 1, R = 5, K = 2;
        int arr[] = { 1, 2, 1, 4, 6, 4, 4 };
        System.out.print(maxDistinctElements(arr, K, L, R, N) + "\n");
 
        // Test case 2
        K = 1;
        System.out.print(maxDistinctElements(arr, K, L, R, N) + "\n");
    }
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 program for above approach
 
# Function to calculate
# maximum distinct elements possible
# after at most K changes
def maxDistinctElements(arr, K, L, R, n):
   
    # Map to store frequency of all the elements
    frequency = {}
 
    # Count frequency of each element
    for x in range(n):
        if arr[x] in frequency:
            frequency[arr[x]] += 1
        else:
            frequency[arr[x]] = 1
         
    # To store number of extra elements
    # that needs to be changed
    extra = (n - len(frequency))
 
    # Traverse from L to R
    # and see which number is not
    # present in map, use that number
    # to change extra duplicate element
    i = L
    while(i <= R and K != 0 and extra != 0):
        if (i not in frequency):
            frequency[i] = 1
            K -= 1
            extra -= 1
        else:
            frequency[i] = 1
             
        i += 1
 
    # Total distinct element will be equal
    # to the size of updated frequency map.
    ans = len(frequency)
 
    # Return answer
    return ans
 
# Driver Code
if __name__ == '__main__':
   
    # Test case 1
    N = 7
    L = 1
    R = 5
    K = 2
    arr = [1, 2, 1, 4, 6, 4, 4]
    print(maxDistinctElements(arr, K, L, R, N))
 
    # Test case 2
    K = 1
    print(maxDistinctElements(arr, K, L, R, N))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for above approach
using System;
using System.Collections.Generic;
class GFG {
    // Function to calculate
    // maximum distinct elements possible
    // after at most K changes
    static int maxDistinctElements(int[] arr, int K, int L,
                                   int R, int n)
    {
        // Map to store frequency of all the elements
        Dictionary frequency
            = new Dictionary();
 
        // Count frequency of each element
        for (int x = 0; x < n; x++) {
            if (frequency.ContainsKey(arr[x]))
                frequency[arr[x]] += 1;
            else
                frequency[arr[x]] = 1;
        }
 
        // To store number of extra elements
        // that needs to be changed
        int extra = (n - frequency.Count);
 
        // Traverse from L to R
        // and see which number is not
        // present in map, use that number
        // to change extra duplicate element
        for (int i = L; i <= R && K != 0 && extra != 0;
             i++) {
            if (!frequency.ContainsKey(i)) {
                frequency[i] = 1;
                K--;
                extra--;
            }
        }
 
        // Total distinct element will be equal
        // to the size of updated frequency map.
        int ans = frequency.Count;
 
        // Return answer
        return ans;
    }
 
    // Driver Code
    public static void Main()
    {
        // Test case 1
        int N = 7, L = 1, R = 5, K = 2;
        int[] arr = { 1, 2, 1, 4, 6, 4, 4 };
        Console.WriteLine(
            maxDistinctElements(arr, K, L, R, N));
 
        // Test case 2
        K = 1;
        Console.WriteLine(
            maxDistinctElements(arr, K, L, R, N));
    }
}
 
// Tis code is contributed by decode2207.


Javascript



输出:
6
5

时间复杂度: O(N)。

辅助空间: O(N)。