给定长度为N的二进制字符串str和整数K ,其中K在(1≤K≤N)范围内,任务是找到所需的最小翻转次数(将0 s转换为1或反之亦然)在给定的字符串上执行,以使所得的字符串在一起不包含K个或多个零。
例子:
Input: str = “11100000011”, K = 3
Output: 2
Explanation:
Flipping 6th and 7th characters modifies the string to “11100110011”.
Therefore, no substring of 0s are present in the string having length 3 or more.
Input: str = “110011”, K = 1
Output: 2
Flip 3rd and 4th characters then str = “111111” which does not contain 1 or more zeros together.
天真的方法:最简单的方法是生成给定字符串的所有可能的子字符串,对于每个子字符串,检查它是否仅由0组成。如果发现是真的,请检查其长度是否超过K。如果发现为真,则计算该子字符串所需的翻转次数。最后,打印获得的翻转次数。
时间复杂度: O(2 N )
辅助空间: O(N)
高效方法:要使结果字符串的连续零计数少于K ,请选择仅由长度≥K的零组成的子字符串。因此,问题减少到在每个子串中找到最小的翻转。最终结果将是所有此类子段的最小翻转总和。步骤如下:
- 将结果初始化为0和连续零的计数(例如cnt_zeros ) 到0 。
- 横切字符串,遇到‘0’时cnt_zeros递增1 。
- 如果cnt_zeros等于K,则增加结果并将cnt_zeros设置为0 。
- 并且当“ 1”到来时,将cnt_zeros设置为0,因为连续零的计数应变为0 。
下面执行上述方法:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to return minimum
// number of flips required
int min_flips(string& str, int k)
{
// Base Case
if (str.size() == 0)
return 0;
// Stores the count of
// minimum number of flips
int ans = 0;
// Stores the count of zeros
// in current substring
int cnt_zeros = 0;
for (char ch : str) {
// If current character is 0
if (ch == '0') {
// Continue ongoing
// substring
++cnt_zeros;
}
else {
// Start a new substring
cnt_zeros = 0;
}
// If k consecutive
// zeroes are obtained
if (cnt_zeros == k) {
++ans;
// End segment
cnt_zeros = 0;
}
}
// Return the result
return ans;
}
// Driver Code
int main()
{
string str = "11100000011";
int k = 3;
// Function call
cout << min_flips(str, k);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to return minimum
// number of flips required
static int min_flips(String str, int k)
{
// Base Case
if (str.length() == 0)
return 0;
// Stores the count of
// minimum number of flips
int ans = 0;
// Stores the count of zeros
// in current subString
int cnt_zeros = 0;
for(char ch : str.toCharArray())
{
// If current character is 0
if (ch == '0')
{
// Continue ongoing
// subString
++cnt_zeros;
}
else
{
// Start a new subString
cnt_zeros = 0;
}
// If k consecutive
// zeroes are obtained
if (cnt_zeros == k)
{
++ans;
// End segment
cnt_zeros = 0;
}
}
// Return the result
return ans;
}
// Driver Code
public static void main(String[] args)
{
String str = "11100000011";
int k = 3;
// Function call
System.out.print(min_flips(str, k));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to implement
# the above approach
# Function to return minimum
# number of flips required
def min_flips(strr, k):
# Base Case
if (len(strr) == 0):
return 0
# Stores the count of
# minimum number of flips
ans = 0
# Stores the count of zeros
# in current sub
cnt_zeros = 0
for ch in strr:
# If current character is 0
if (ch == '0'):
# Continue ongoing
# sub
cnt_zeros += 1
else:
# Start a new sub
cnt_zeros = 0
# If k consecutive
# zeroes are obtained
if (cnt_zeros == k):
ans += 1
# End segment
cnt_zeros = 0
# Return the result
return ans
# Driver Code
if __name__ == '__main__':
strr = "11100000011"
k = 3
# Function call
print(min_flips(strr, k))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to return minimum
// number of flips required
static int min_flips(String str, int k)
{
// Base Case
if (str.Length == 0)
return 0;
// Stores the count of
// minimum number of flips
int ans = 0;
// Stores the count of zeros
// in current subString
int cnt_zeros = 0;
foreach(char ch in str.ToCharArray())
{
// If current character is 0
if (ch == '0')
{
// Continue ongoing
// subString
++cnt_zeros;
}
else
{
// Start a new subString
cnt_zeros = 0;
}
// If k consecutive
// zeroes are obtained
if (cnt_zeros == k)
{
++ans;
// End segment
cnt_zeros = 0;
}
}
// Return the result
return ans;
}
// Driver Code
public static void Main(String[] args)
{
String str = "11100000011";
int k = 3;
// Function call
Console.Write(min_flips(str, k));
}
}
// This code is contributed by 29AjayKumar
2
时间复杂度: O(N)
辅助空间: O(1)