📌  相关文章
📜  以 0 为多数元素的给定三元数组中子数组的最小长度

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

以 0 为多数元素的给定三元数组中子数组的最小长度

给定一个大小为n的整数数组arr[] ,其中只有三种整数0、12 。找到长度>=2的数组arr[]的子数组的最小长度,使得0 的频率大于12 的频率。如果找不到打印-1

朴素方法:这个问题可以通过遍历每个子数组并检查0、12 的频率并检查0 的频率是否大于21然后存储子数组的最小长度作为答案来完成。

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

有效方法:由于只有 3 种类型的整数,因此满足上述条件的长度>=2的可能子数组将是

子数组的最大可能最小长度为7 。满足上述条件且长度 > 7 的任何其他子数组将包含上述任何子数组,因此满足上述条件的子数组的最大可能长度为 7。

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

  • 将变量min_length初始化为 INT_MAX
  • 使用变量i[0, n)范围内迭代并执行以下任务:
    • 使用初始频率0初始化数组count[]
    • 使用count[arr[i]]++增加arr[i]的频率。
    • 使用变量j[i+1, min(n, i+7))范围内迭代并执行以下任务:
      • 通过使用每个大小<=7子数组的count[arr[j]]++来增加arr[j]的频率。
      • 如果count[0]大于count[1]count[2]并且如果min_length > j-i+1则分配min_length = j-i+1
  • 如果min_length等于INT_MAX返回-1。
  • 否则打印min_length作为答案。

下面是上述方法的实现。

C++
// C++ code for the above approach
#include 
using namespace std;
 
// Function to find the minimum
// subarray length with most 0's
int minLength(vector arr)
{
    int min_len = INT_MAX;
    int n = arr.size();
 
    // Traverse the array to check
    // the required condition
    for (int i = 0; i < n; i++) {
 
        // Initialize a vector count
        // to store the frequencies
        int count[3] = { 0 };
        count[arr[i]]++;
 
        // Check all subarrays of length
        // <=7 and count the frequencies
        for (int j = i + 1; j < min(n, i + 7); j++) {
            count[arr[j]]++;
 
            // If the frequency of 0's is
            // greater than both 1's and 2's
            // then take length of minimum subarray
            if (count[0] > count[1]
                && count[0] > count[2])
                min_len = min(min_len, j - i + 1);
        }
    }
 
    // If min_len == INT_MAX we have no subarray
    // satisfying the condition return -1;
    if (min_len == INT_MAX)
        min_len = -1;
 
    return min_len;
}
 
// Driver Code
int main()
{
 
    // Initializing list of nums
    vector arr = { 2, 0, 2, 0, 1, 2, 2, 2 };
 
    // Call the function and print the answer
    cout << (minLength(arr));
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG {
 
    // Function to find the minimum
    // subarray length with most 0's
    static int minLength(ArrayList arr)
    {
        int min_len = Integer.MAX_VALUE;
        int n = arr.size();
 
        // Traverse the array to check
        // the required condition
        for (int i = 0; i < n; i++) {
 
            // Initialize a vector count
            // to store the frequencies
            int[] count = new int[3];
            for (int j = 0; j < 3; j++) {
                count[j] = 0;
            }
 
            int x = (int)arr.get(i);
            count[x]++;
 
            // Check all subarrays of length
            // <=7 and count the frequencies
            for (int j = i + 1; j < Math.min(n, i + 7);
                 j++) {
                int y = (int)arr.get(j);
                count[y]++;
 
                // If the frequency of 0's is
                // greater than both 1's and 2's
                // then take length of minimum subarray
                if (count[0] > count[1]
                    && count[0] > count[2])
                    min_len = Math.min(min_len, j - i + 1);
            }
        }
 
        // If min_len == INT_MAX we have no subarray
        // satisfying the condition return -1;
        if (min_len == Integer.MAX_VALUE)
            min_len = -1;
 
        return min_len;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        ArrayList arr = new ArrayList();
 
        arr.add(2);
        arr.add(0);
        arr.add(2);
        arr.add(0);
        arr.add(1);
        arr.add(2);
        arr.add(2);
        arr.add(2);
 
        // Count of isograms in string array arr[]
        System.out.println(minLength(arr));
    }
}
 
// This code is contributed by ukasp.


Python3
# python code for the above approach
INT_MAX = 2147483647
 
# Function to find the minimum
# subarray length with most 0's
def minLength(arr):
 
    min_len = INT_MAX
    n = len(arr)
 
    # Traverse the array to check
    # the required condition
    for i in range(0, n):
 
        # Initialize a vector count
        # to store the frequencies
        count = [0, 0, 0]
        count[arr[i]] += 1
 
        # Check all subarrays of length
        # <=7 and count the frequencies
        for j in range(i+1, min(n, i+7)):
            count[arr[j]] += 1
 
            # If the frequency of 0's is
            # greater than both 1's and 2's
            # then take length of minimum subarray
            if (count[0] > count[1] and count[0] > count[2]):
                min_len = min(min_len, j - i + 1)
 
    # If min_len == INT_MAX we have no subarray
    # satisfying the condition return -1;
    if (min_len == INT_MAX):
        min_len = -1
 
    return min_len
 
# Driver Code
if __name__ == "__main__":
 
    # Initializing list of nums
    arr = [2, 0, 2, 0, 1, 2, 2, 2]
 
    # Call the function and print the answer
    print(minLength(arr))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections;
 
class GFG{
 
// Function to find the minimum
// subarray length with most 0's
static int minLength(ArrayList arr)
{
    int min_len = Int32.MaxValue;
    int n = arr.Count;
 
    // Traverse the array to check
    // the required condition
    for (int i = 0; i < n; i++) {
 
        // Initialize a vector count
        // to store the frequencies
        int []count = new int[3];
        for(int j = 0; j < 3; j++) {
            count[j] = 0;
        }
         
        int x = (int)arr[i];
        count[x]++;
 
        // Check all subarrays of length
        // <=7 and count the frequencies
        for (int j = i + 1; j < Math.Min(n, i + 7); j++) {
          int y = (int)arr[j];
            count[y]++;
 
            // If the frequency of 0's is
            // greater than both 1's and 2's
            // then take length of minimum subarray
            if (count[0] > count[1]
                && count[0] > count[2])
                min_len =   Math.Min(min_len, j - i + 1);
        }
    }
 
    // If min_len == INT_MAX we have no subarray
    // satisfying the condition return -1;
    if (min_len == Int32.MaxValue)
        min_len = -1;
 
    return min_len;
}
 
// Driver Code
public static void Main()
{
    ArrayList arr = new ArrayList();
     
    arr.Add(2);
    arr.Add(0);
    arr.Add(2);
    arr.Add(0);
    arr.Add(1);
    arr.Add(2);
    arr.Add(2);
    arr.Add(2);
 
    // Count of isograms in string array arr[]
    Console.WriteLine(minLength(arr));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
3

时间复杂度: O(n)
辅助空间: O(1)