📜  查找前缀的最大长度

📅  最后修改于: 2021-04-21 23:51:26             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,其中数组的所有元素都在[0,9]范围内即一位数字,任务是找到此数组的前缀的最大长度,以便从中完全删除一个元素前缀将使前缀中其余元素的出现相同。
例子:

方法:遍历所有前缀,并检查每个前缀是否可以删除一个元素,以便每个元素都出现相同的位置。为了满足此条件,必须满足以下条件之一:

  • 前缀中只有一个元素。
  • 前缀中的所有元素都出现1。
  • 每个元素都具有相同的出现,但只有一个元素的出现为1。
  • 每个元素都具有相同的出现次数,但恰好一个元素的出现次数比其他任何元素多1个。

下面是上述方法的实现:

C++14
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum
// length of the required prefix
int Maximum_Length(vector a)
{
     
    // Array to store the frequency
    // of each element of the array
    int counts[11] = {0};
 
    // Iterating for all the elements
    int ans = 0;
    for(int index = 0;
            index < a.size();
            index++)
    {
         
        // Update the frequency of the
        // current element i.e. v
        counts[a[index]] += 1;
 
        // Sorted positive values
        // from counts array
        vector k;
        for(auto i : counts)
            if (i != 0)
                k.push_back(i);
 
        sort(k.begin(), k.end());
 
        // If current prefix satisfies
        // the given conditions
        if (k.size() == 1 ||
           (k[0] == k[k.size() - 2] &&
            k.back() - k[k.size() - 2] == 1) ||
           (k[0] == 1 and k[1] == k.back()))
            ans = index;
    }
     
    // Return the maximum length
    return ans + 1;
}
 
// Driver code
int main()
{
    vector a = { 1, 1, 1, 2, 2, 2 };
 
    cout << (Maximum_Length(a));
}
 
// This code is contributed by grand_master


Java
// Java implementation of the approach
import java.util.*;
public class Main
{
    // Function to return the maximum
    // length of the required prefix
    public static int Maximum_Length(Vector a)
    {
           
        // Array to store the frequency
        // of each element of the array
        int[] counts = new int[11];
       
        // Iterating for all the elements
        int ans = 0;
        for(int index = 0;
                index < a.size();
                index++)
        {
               
            // Update the frequency of the
            // current element i.e. v
            counts[a.get(index)] += 1;
       
            // Sorted positive values
            // from counts array
            Vector k = new Vector();
            for(int i : counts)
                if (i != 0)
                    k.add(i);
       
            Collections.sort(k); 
       
            // If current prefix satisfies
            // the given conditions
            if (k.size() == 1 ||
               (k.get(0) == k.get(k.size() - 2) &&
                k.get(k.size() - 1) - k.get(k.size() - 2) == 1) ||
               (k.get(0) == 1 && k.get(1) == k.get(k.size() - 1)))
                ans = index;
        }
           
        // Return the maximum length
        return ans + 1;
    }
     
    // Driver code
    public static void main(String[] args) {
        Vector a = new Vector();
        a.add(1);
        a.add(1);
        a.add(1);
        a.add(2);
        a.add(2);
        a.add(2);
        
        System.out.println(Maximum_Length(a));
    }
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation of the approach
 
# Function to return the maximum
# length of the required prefix
def Maximum_Length(a):
 
    # Array to store the frequency
    # of each element of the array
    counts =[0]*11
 
    # Iterating for all the elements
    for index, v in enumerate(a):
 
        # Update the frequency of the
        # current element i.e. v
        counts[v] += 1
 
        # Sorted positive values from counts array
        k = sorted([i for i in counts if i])
 
        # If current prefix satisfies
        # the given conditions
        if len(k)== 1 or (k[0]== k[-2] and k[-1]-k[-2]== 1) or (k[0]== 1 and k[1]== k[-1]):
            ans = index
 
    # Return the maximum length
    return ans + 1
 
# Driver code
if __name__=="__main__":
    a = [1, 1, 1, 2, 2, 2]
    n = len(a)
    print(Maximum_Length(a))


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to return the maximum
    // length of the required prefix
    static int Maximum_Length(List a)
    {
          
        // Array to store the frequency
        // of each element of the array
        int[] counts = new int[11];
      
        // Iterating for all the elements
        int ans = 0;
        for(int index = 0;
                index < a.Count;
                index++)
        {
              
            // Update the frequency of the
            // current element i.e. v
            counts[a[index]] += 1;
      
            // Sorted positive values
            // from counts array
            List k = new List();
            foreach(int i in counts)
                if (i != 0)
                    k.Add(i);
      
            k.Sort();
      
            // If current prefix satisfies
            // the given conditions
            if (k.Count == 1 ||
               (k[0] == k[k.Count - 2] &&
                k[k.Count - 1] - k[k.Count - 2] == 1) ||
               (k[0] == 1 && k[1] == k[k.Count - 1]))
                ans = index;
        }
          
        // Return the maximum length
        return ans + 1;
    }
 
  static void Main() {
    List a = new List(new int[]{ 1, 1, 1, 2, 2, 2 });
    Console.Write(Maximum_Length(a));
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出:
5