通过重复翻转大小为 K 的子字符串中的字符,最小化翻转以使二进制字符串全为 1
给定一个大小为N的二进制字符串S和一个整数K ,任务是通过翻转大小为 K的子字符串中的字符来找到使二进制字符串中的所有字符都为1所需的最小操作数。如果无法这样做,则打印“-1” 。
例子:
Input: S = “00010110 “, K = 3
Output: 3
Explanation:
Below are the operations performed:
- Consider the substring S[0, 2] which is of size K(= 3) and flipping those characters modifies the string to “11110110”.
- Consider the substring S[4, 6] which is of size K(= 3) and flipping those characters modifies the string to “11111000”.
- Consider the substring S[5, 7] which is of size K(= 3) and flipping those characters modifies the string to “11111111”.
After the above operations, all the 0s are converted to 1s, and the minimum number of operations required is 3.
Input: S = “01010”, K = 4
Output: -1
方法:给定的问题可以使用贪心方法来解决。请按照以下步骤解决问题:
- 初始化一个变量,例如minOperations为0 ,它存储所需的最小操作数的计数。
- 使用变量i遍历字符串S ,如果字符S[i]为'0'并且(i + K)的值最多为 K ,则翻转子字符串S[i, i + K]的所有字符并将minOperations的值增加1 。
- 完成上述操作后,遍历字符串S ,如果存在字符'0',则打印“-1”并跳出循环。
- 如果字符串S的所有字符都是1s ,则打印minOperations作为结果的最小操作数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of operations required to convert all
// the characters to 1 by flipping the
// substrings of size K
void minOperation(string S, int K, int N)
{
// Stores the minimum number of
// operations required
int min = 0;
int i;
// Traverse the string S
for (i = 0; i < N; i++) {
// If the character is 0
if (S[i] == '0' && i + K <= N) {
// Flip the substrings of
// size K starting from i
for (int j = i; j < i + K; j++) {
if (S[j] == '1')
S[j] = '0';
else
S[j] = '1';
}
// Increment the minimum count
// of operations required
min++;
}
}
// After performing the operations
// check if string S contains any 0s
for (i = 0; i < N; i++) {
if (S[i] == '0')
break;
}
// If S contains only 1's
if (i == N)
cout << min;
else
cout << -1;
}
// Driver Code
int main()
{
string S = "00010110";
int K = 3;
int N = S.length();
minOperation(S, K, N);
return 0;
}
Java
// Java program for the above approach
public class GFG
{
// Function to find the minimum number
// of operations required to convert all
// the characters to 1 by flipping the
// substrings of size K
static void minOperation(String S, int K, int N)
{
// Stores the minimum number of
// operations required
int min = 0;
int i;
// Traverse the string S
for (i = 0; i < N; i++) {
// If the character is 0
if (S.charAt(i) == '0' && i + K <= N) {
// Flip the substrings of
// size K starting from i
for (int j = i; j < i + K; j++) {
if (S.charAt(j) == '1')
S = S.substring(0, j) + '0' + S.substring(j + 1);
else
S = S.substring(0, j) + '1' + S.substring(j + 1);
}
// Increment the minimum count
// of operations required
min++;
}
}
// After performing the operations
// check if string S contains any 0s
for (i = 0; i < N; i++) {
if (S.charAt(i) == '0')
break;
}
// If S contains only 1's
if (i == N)
System.out.println(min);
else
System.out.println(-1);
}
// Driver Code
public static void main(String []args)
{
String S = "00010110";
int K = 3;
int N = S.length();
minOperation(S, K, N);
}
}
// This code is contributed by AnkThon
Python3
# Function to find the minimum number
# of operations required to convert all
# the characters to 1 by flipping the
# substrings of size K
def minOperation(St, K, N):
S = list(St)
# Stores the minimum number of
# operations required
min = 0
i = 0
# Traverse the string S
for i in range(0, N):
# If the character is 0
if (S[i] == '0' and i + K <= N):
# Flip the substrings of
# size K starting from i
j = i
while(j < i + K):
if (S[j] == '1'):
S[j] = '0'
else:
S[j] = '1'
j += 1
# Increment the minimum count
# of operations required
min += 1
# After performing the operations
# check if string S contains any 0s
temp = 0
for i in range(0, N):
temp += 1
if (S[i] == '0'):
break
# If S contains only 1's
if (temp == N):
print(min)
else:
print(-1)
# Driver Code
S = "00010110"
K = 3
N = len(S)
minOperation(S, K, N)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum number
// of operations required to convert all
// the characters to 1 by flipping the
// substrings of size K
static void minOperation(string S, int K, int N)
{
// Stores the minimum number of
// operations required
int min = 0;
int i;
// Traverse the string S
for (i = 0; i < N; i++) {
// If the character is 0
if (S[i] == '0' && i + K <= N) {
// Flip the substrings of
// size K starting from i
for (int j = i; j < i + K; j++) {
if (S[j] == '1')
S = S.Substring(0, j) + '0' + S.Substring(j + 1);
else
S = S.Substring(0, j) + '1' + S.Substring(j + 1);
}
// Increment the minimum count
// of operations required
min++;
}
}
// After performing the operations
// check if string S contains any 0s
for (i = 0; i < N; i++) {
if (S[i] == '0')
break;
}
// If S contains only 1's
if (i == N)
Console.Write(min);
else
Console.Write(-1);
}
// Driver Code
public static void Main()
{
string S = "00010110";
int K = 3;
int N = S.Length;
minOperation(S, K, N);
}
}
// This code is contributed by SURENDRA_GANGWAR.
Javascript
输出:
3
时间复杂度: O(N*K)
辅助空间: O(1)