📌  相关文章
📜  要删除的最小元素以使 0 和 1 的计数相等

📅  最后修改于: 2021-09-07 02:24:10             🧑  作者: Mango

给定两个长度相等的二进制数组AB ,任务是从前面找到最少要删除的元素数,使给定的两个二进制数组中 0 和 1 的计数相等。

例子:

方法:想法是分别找出1的总数和0的总数之间的差异,并将这种差异最小化。删除 1 将使差异减少 1,而删除 0 将使差异增加 1。
因此,使差值等于 0 所需的步骤是:

  • 计算1 和 0 总数的初始差异。我们称之为initial_diff
  • 从第一个数组中删除前 l 个元素并将差异存储在变量left_diff 中
  • 从第二个数组中删除前 r 个元素并将差异存储在变量 right_diff 中。
  • 现在找到这样的 l 和 r 值,使得:
  • 为了最优地找到 l 和 r,对于 right_diff 的每个唯一值,在 unordered_map 中保存最小的 r 以达到该值。最后,迭代 l 以找到最小答案。

下面是上述方法的实现

C++
// C++ program to find minimum elements
// to be removed to make count
// of 0s and 1s equal
 
#include 
using namespace std;
 
// Function that returns minimum elements
// need to be removed
int MinRemovedElements(int a[], int b[],
                    int n)
{
 
    int no_of_ones = 0;
    int no_of_zeroes = 0;
 
    // Counting total number of
    // zeroes and ones
    for (int i = 0; i < n; i++) {
        if (a[i] == 1)
            no_of_ones++;
        else
            no_of_zeroes++;
 
        if (b[i] == 1)
            no_of_ones++;
        else
            no_of_zeroes++;
    }
 
    // Computing difference in ones and
    // zeroes
    int diff = no_of_ones - no_of_zeroes;
 
    unordered_map mp;
    mp[0] = 0;
    int curr = 0;
 
    // Filling the difference of number
    // of 1's and 0's of 1st array in
    // unoredered_map
    for (int i = 0; i < n; i++) {
        if (a[i] == 1)
            curr++;
        else
            curr--;
 
        // Condition if current difference
        // not found in map
        if (mp.find(curr) == mp.end())
            mp[curr] = i + 1;
    }
 
    curr = 0;
    int answer = 2 * n;
 
    for (int i = 0; i < n; i++) {
        if (b[i] == 1)
            curr++;
        else
            curr--;
 
        // Condition if current difference is
        // present in map then compute the
        // minimum one
        if (mp.find(diff - curr) != mp.end())
            answer
                = min(answer,
                    i + 1 + mp);
    }
 
    // Condition if the total difference
    // is present in 1st array
    if (mp.find(diff) != mp.end())
        answer = min(answer, mp);
 
    return answer;
}
 
// Driver Code
int main()
{
    int a[] = { 0, 1, 0, 1, 1, 0 };
    int b[] = { 1, 0, 0, 1, 1, 1 };
    int size = sizeof(a) / sizeof(a[0]);
 
    cout << MinRemovedElements(a, b, size)
        << "\n";
 
    return 0;
}


Java
// Java program to find minimum elements
// to be removed to make count
// of 0s and 1s equal
import java.util.*;
 
class GFG{
 
// Function that returns minimum elements
// need to be removed
static int MinRemovedElements(int a[], int b[],
                    int n)
{
 
    int no_of_ones = 0;
    int no_of_zeroes = 0;
 
    // Counting total number of
    // zeroes and ones
    for (int i = 0; i < n; i++) {
        if (a[i] == 1)
            no_of_ones++;
        else
            no_of_zeroes++;
 
        if (b[i] == 1)
            no_of_ones++;
        else
            no_of_zeroes++;
    }
 
    // Computing difference in ones and
    // zeroes
    int diff = no_of_ones - no_of_zeroes;
 
    HashMap mp = new HashMap();
    mp.put(0, 0);
    int curr = 0;
 
    // Filling the difference of number
    // of 1's and 0's of 1st array in
    // unoredered_map
    for (int i = 0; i < n; i++) {
        if (a[i] == 1)
            curr++;
        else
            curr--;
 
        // Condition if current difference
        // not found in map
        if (!mp.containsKey(curr))
            mp.put(curr, i + 1);
    }
 
    curr = 0;
    int answer = 2 * n;
 
    for (int i = 0; i < n; i++) {
        if (b[i] == 1)
            curr++;
        else
            curr--;
 
        // Condition if current difference is
        // present in map then compute the
        // minimum one
        if (mp.containsKey(diff - curr))
            answer
                = Math.min(answer,mp.get(diff - curr) + 1 + i);
    }
 
    // Condition if the total difference
    // is present in 1st array
    if (mp.containsKey(diff))
        answer = Math.min(answer, mp.get(diff));
 
    return answer;
}
 
// Driver Code
public static void main(String[] args)
{
    int a[] = { 0, 1, 0, 1, 1, 0 };
    int b[] = { 1, 0, 0, 1, 1, 1 };
    int size = a.length;
 
    System.out.print(MinRemovedElements(a, b, size)
        + "\n");
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find minimum
# elements to be removed to make
# count of 0s and 1s equal
 
# Function that returns minimum
# elements need to be removed
def MinRemovedElements(a, b, n):
 
    no_of_ones = 0;
    no_of_zeroes = 0;
 
    # Counting total number of
    # zeroes and ones
    for i in range(n):
        if (a[i] == 1):
            no_of_ones += 1;
        else:
            no_of_zeroes += 1;
 
        if (b[i] == 1):
            no_of_ones += 1;
        else:
            no_of_zeroes += 1;
 
    # Computing difference in ones and
    # zeroes
    diff1 = no_of_ones - no_of_zeroes;
 
    mp = {};
    mp[0] = 0;
    curr = 0;
 
    # Filling the difference of number
    # of 1's and 0's of 1st array in
    # unoredered_map
    for i in range(n):
        if (a[i] == 1):
            curr += 1;
        else :
            curr -= 1;
 
        # Condition if current difference
        # not found in map
        if curr not in mp:
            mp[curr] = i + 1;
     
    curr = 0;
    answer = 2 * n;
 
    for i in range(n):
        if (b[i] == 1):
            curr += 1;
        else:
            curr -= 1;
 
        # Condition if current difference is
        # present in map then compute the
        # minimum one
        if (diff1 - curr) in mp :
            answer = min(answer, i + 1 + mp[diff1 -
                                            curr]);
 
    # Condition if the total difference
    # is present in 1st array
    if diff1 in mp:
        answer = min(answer, mp[diff1]);
 
    return answer;
 
# Driver Code
if __name__ == "__main__" :
 
    a = [ 0, 1, 0, 1, 1, 0 ];
    b = [ 1, 0, 0, 1, 1, 1 ];
     
    size = len(a);
 
    print(MinRemovedElements(a, b, size));
     
# This code is contributed by Yash_R


C#
// C# program to find minimum elements
// to be removed to make count
// of 0s and 1s equal
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function that returns minimum elements
// need to be removed
static int MinRemovedElements(int[] a, int[] b,
                              int n)
{
    int no_of_ones = 0;
    int no_of_zeroes = 0;
   
    // Counting total number of
    // zeroes and ones
    for(int i = 0; i < n; i++)
    {
        if (a[i] == 1)
            no_of_ones++;
        else
            no_of_zeroes++;
   
        if (b[i] == 1)
            no_of_ones++;
        else
            no_of_zeroes++;
    }
   
    // Computing difference in ones and
    // zeroes
    int diff = no_of_ones - no_of_zeroes;
     
    Dictionary mp = new Dictionary();
                                         
    mp.Add(0, 0);
    int curr = 0;
   
    // Filling the difference of number
    // of 1's and 0's of 1st array in
    // unoredered_map
    for(int i = 0; i < n; i++)
    {
        if (a[i] == 1)
            curr++;
        else
            curr--;
   
        // Condition if current difference
        // not found in map
        if (!mp.ContainsKey(curr))
            mp.Add(curr, i + 1);
    }
   
    curr = 0;
    int answer = 2 * n;
   
    for(int i = 0; i < n; i++)
    {
        if (b[i] == 1)
            curr++;
        else
            curr--;
   
        // Condition if current difference is
        // present in map then compute the
        // minimum one
        if (mp.ContainsKey(diff - curr))
            answer = Math.Min(answer,
             i + 1 + mp);
    }
   
    // Condition if the total difference
    // is present in 1st array
    if (mp.ContainsKey(diff))
        answer = Math.Min(answer, mp);
   
    return answer;
}
 
// Driver code
static void Main()
{
    int[] a = { 0, 1, 0, 1, 1, 0 };
    int[] b = { 1, 0, 0, 1, 1, 1 };
    int size = a.Length;
      
    Console.WriteLine(MinRemovedElements(
        a, b, size));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:

6

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live