给定一个长度为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
Javascript
2
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live