📌  相关文章
📜  要选择的最小 0 计数,以便所有 1 都与它们相邻

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

要选择的最小 0 计数,以便所有 1 都与它们相邻

给定一个大小为N的二进制字符串str ,其每个字符都是'1''0' 。任务是选择最少数量的0 ,以便为每个'1'选择至少一个邻居。打印所选0'计数

例子:

方法:解决方案基于贪婪方法。请按照以下步骤获取解决方案。

  • 从头开始迭代字符串。
  • 对于每个“1”,如果可能,从其邻域中选择一个“0”。
  • 现在,如果在当前'1'之前和之后都有'0' ,则始终选择当前 '1' 旁边的邻居(因为在此之后可以有更多 '1' 这样做将允许选择最小数量邻居)。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to count
// minimum number of buckets
int minimumBuckets(string str)
{
    int bucketcount = 0;
    int N = str.size();
 
    // Loop to count minimum buckets
    for (int i = 0; i < N;) {
        if (str[i] == '1') {
             
            // If bucket can be put,
            // no need of next two indices,
            // so shift to i+3
            if (i + 1 < N &&
                str[i + 1] == '0') {
                bucketcount++;
                i += 3;
                continue;
            }
            if (i - 1 >= 0 &&
                str[i - 1] == '0') {
                bucketcount++;
                i++;
                continue;
            }
            return -1;
        }
        i++;
    }
    return bucketcount;
}
 
// Driver code
int main()
{
    string str = "1001";
    cout << minimumBuckets(str)<


Java
// Java code to implement the above approach
class GFG {
 
    // Function to count
    // minimum number of buckets
    static int minimumBuckets(String str) {
        int bucketcount = 0;
        int N = str.length();
 
        // Loop to count minimum buckets
        for (int i = 0; i < N;) {
            if (str.charAt(i) == '1') {
 
                // If bucket can be put,
                // no need of next two indices,
                // so shift to i+3
                if (i + 1 < N && str.charAt(i + 1) == '0') {
                    bucketcount++;
                    i += 3;
                    continue;
                }
                if (i - 1 >= 0 && str.charAt(i - 1) == '0') {
                    bucketcount++;
                    i++;
                    continue;
                }
                return -1;
            }
            i++;
        }
        return bucketcount;
    }
 
    // Driver code
    public static void main(String args[]) {
        String str = "1001";
        System.out.println(minimumBuckets(str));
 
        String str1 = "1010";
        System.out.println(minimumBuckets(str1));
    }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# python code to implement the above approach
 
# Function to count
# minimum number of buckets
def minimumBuckets(str):
 
    bucketcount = 0
    N = len(str)
 
    # Loop to count minimum buckets
    i = 0
    while(i < N):
        if (str[i] == '1'):
 
            # If bucket can be put,
            # no need of next two indices,
            # so shift to i+3
            if (i + 1 < N and str[i + 1] == '0'):
                bucketcount += 1
                i += 3
                continue
 
            if (i - 1 >= 0 and str[i - 1] == '0'):
                bucketcount += 1
                i += 1
                continue
 
            return -1
 
        i += 1
 
    return bucketcount
 
# Driver code
if __name__ == "__main__":
 
    str = "1001"
    print(minimumBuckets(str))
 
    str1 = "1010"
    print(minimumBuckets(str1))
 
    # This code is contributed by rakeshsahni


C#
// C# code to implement the above approach
using System;
class GFG {
 
    // Function to count
    // minimum number of buckets
    static int minimumBuckets(string str)
    {
        int bucketcount = 0;
        int N = str.Length;
 
        // Loop to count minimum buckets
        for (int i = 0; i < N;) {
            if (str[i] == '1') {
 
                // If bucket can be put,
                // no need of next two indices,
                // so shift to i+3
                if (i + 1 < N && str[i + 1] == '0') {
                    bucketcount++;
                    i += 3;
                    continue;
                }
                if (i - 1 >= 0 && str[i - 1] == '0') {
                    bucketcount++;
                    i++;
                    continue;
                }
                return -1;
            }
            i++;
        }
        return bucketcount;
    }
 
    // Driver code
    public static void Main()
    {
        string str = "1001";
        Console.WriteLine(minimumBuckets(str));
 
        string str1 = "1010";
        Console.WriteLine(minimumBuckets(str1));
    }
}
 
// This code is contributed by ukasp.


Javascript



输出
2
1

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