📌  相关文章
📜  找到使数组良好应删除的最小元素数

📅  最后修改于: 2021-04-23 05:30:32             🧑  作者: Mango

给定大小为N的数组和整数K。该数组仅包含数字{0,1,2,3,…k-1}。我们的任务是通过删除一些元素来使数组变好。如果x可以被k整除,并且可以将给定的数组拆分为x / k个子序列,且每个子集的形式为{0,1,2,3,…k-1},则将长度为x的数组称为良。
注意:空数组也是一个好的数组
例子:

方法:

  • 令cnt 0为[0]的子序列数,令cnt 1为子序列[0,1]的数,cnt 2-子序列数[0,1,2]等等,而cnt k-1为完成的子序列数[0、1、2、3,…k-1]。
  • 从左到右依次遍历arr的所有元素。如果数组中的当前元素为零,则将cnt 0的计数增加1。
  • 如果数组中的当前元素不为零,则检查其在序列计数中的前一个元素是否大于零。
  • 如果其序列中的前一个元素大于零,则将前一个元素的计数减一,并将当前元素的计数减一。

下面是上述方法的实现:

C++
// C++ program to remove minimum elements to
// make the given array good
#include 
using namespace std;
  
// Function to remove minimum elements to
// make the given array good
int MinRemove(int a[], int n, int k)
{
    // To store count of each subsequence
    vector cnt(k, 0);
  
    for (int i = 0; i < n; i++) {
        // Increase the count of subsequence [0]
        if (a[i] == 0)
            cnt[0]++;
  
        // If Previous element subsequence count
        // is greater than zero then increment
        // subsequence count of current element
        // and decrement subsequence count of
        // the previous element.
        else if (cnt[a[i] - 1] > 0) {
            cnt[a[i] - 1]--;
            cnt[a[i]]++;
        }
    }
  
    // Return the required answer
    return n - (k * cnt[k - 1]);
}
  
// Driver code
int main()
{
  
    int a[] = { 0, 1, 2, 3, 4, 0,
                1, 0, 1, 2, 3, 4 },
        k = 5;
  
    int n = sizeof(a) / sizeof(a[0]);
  
    // Function call
    cout << MinRemove(a, n, k);
  
    return 0;
}


Java
// Java program to remove minimum elements to
// make the given array good
import java.util.Collections;
import java.util.Vector;
  
class GFG
{
    // Function to remove minimum elements to
    // make the given array good
    static int MinRemove(int[] a, int n, int k)
    {
        // To store count of each subsequence
        int []cnt = new int[n];
        for (int i = 0; i < n; i++) 
        {
            // Increase the count of subsequence [0]
            if (a[i] == 0)
                cnt[0]++;
  
            // If Previous element subsequence count
            // is greater than zero then increment
            // subsequence count of current element
            // and decrement subsequence count of
            // the previous element.
            else if (cnt[a[i] - 1] > 0)
            {
                cnt[a[i] - 1]--;
                cnt[a[i]]++;
            }
        }
  
        // Return the required answer
        return n - (k * cnt[k - 1]);
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
  
        int a[] = { 0, 1, 2, 3, 4, 0,
                    1, 0, 1, 2, 3, 4 };
        int k = 5;
  
        int n = a.length;
  
        // Function call
        System.out.println(MinRemove(a, n, k));
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 program to remove minimum elements to 
# make the given array good 
  
# Function to remove minimum elements to 
# make the given array good 
def MinRemove(a, n, k) : 
  
    # To store count of each subsequence 
    cnt = [0] * k 
  
    for i in range(n) :
        # Increase the count of subsequence [0] 
        if (a[i] == 0) :
            cnt[0] += 1; 
  
        # If Previous element subsequence count 
        # is greater than zero then increment 
        # subsequence count of current element 
        # and decrement subsequence count of 
        # the previous element. 
        elif (cnt[a[i] - 1] > 0) :
            cnt[a[i] - 1] -= 1; 
            cnt[a[i]] += 1; 
  
    # Return the required answer 
    return n - (k * cnt[k - 1]); 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    a = [ 0, 1, 2, 3, 4, 0, 
                1, 0, 1, 2, 3, 4 ] 
    k = 5; 
  
    n = len(a); 
  
    # Function call 
    print(MinRemove(a, n, k)); 
      
# This code is contributed by AnkitRai01


C#
// C# program to remove minimum elements to
// make the given array good
using System;
  
class GFG
{
    // Function to remove minimum elements to
    // make the given array good
    static int MinRemove(int[] a, int n, int k)
    {
        // To store count of each subsequence
        int []cnt = new int[n];
        for (int i = 0; i < n; i++) 
        {
            // Increase the count of subsequence [0]
            if (a[i] == 0)
                cnt[0]++;
  
            // If Previous element subsequence count
            // is greater than zero then increment
            // subsequence count of current element
            // and decrement subsequence count of
            // the previous element.
            else if (cnt[a[i] - 1] > 0)
            {
                cnt[a[i] - 1]--;
                cnt[a[i]]++;
            }
        }
  
        // Return the required answer
        return n - (k * cnt[k - 1]);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int []a = { 0, 1, 2, 3, 4, 0,
                    1, 0, 1, 2, 3, 4 };
        int k = 5;
        int n = a.Length;
  
        // Function call
        Console.WriteLine(MinRemove(a, n, k));
    }
}
  
// This code is contributed by 29AjayKumar


输出:
2

时间复杂度: O(N)