给定二进制字符串str ,整数A表示将连续的1 s转换为0 s的成本,而B表示将0 s转换为1 s的成本。现在的任务是找到将字符串str减少到0 s的最小开销。
例子:
Input: str = “01101110”, A = 5, B = 1
Output: 6
Explanation:
Convert the str[3] to ‘1’. Cost = 1, str = “01111110”.
Convert the substring {str[1], … str[6]} to ‘0’. Cost = 5, str = “00000000”.
Input: str = “1010010011110111”, A = 12, B = 14
Output: 60
方法:可以根据以下观察结果解决给定问题:
For any pair of consecutive segments of 1s [L1, R1] and [L2, R2] (where L2 > R1), choose to either convert both segments to 0s for a cost of 2 * A or convert 0s between both these segments to 1s and convert [L1, R2] to 1s for a cost of A + (L2 – R1 – 1) * B.
请按照以下步骤解决问题:
- 初始化变量left_1并将最左’1′的索引存储在str中
- 初始化另一个变量right_1 ,并将最右边的’1′的索引存储在str中。
- 如果left_1和right_1不存在,则str仅包含0 s。因此,将0打印为最低费用
- 否则, str中至少有一个“ 1” 。因此,初始化成本= A。
- 使用指针i从左_1到右_1遍历字符串str中的字符
- 计算i右边连续0 s的数量,并将其存储在变量0中。
- 如果零的长度超过0 ,则以零* B的代价将0 s转换为1 s,或者以A的代价将连续的1 s转换为0 s。
- 因此,将成本增加min(zeroes * B,A)
- 最后,打印获得的最低成本。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to find the minimum cost
// to convert given string to 0s only
void convert_to_allzeroes(string str,
int a, int b)
{
// Length of string
int len = str.length();
// Stores the index of leftmost '1' in str
int left_1, i = 0;
while (i < len && str[i] == '0')
i++;
// Update the index of leftmost '1' in str
left_1 = i;
// Stores the index of rightmost '1' in str
int right_1;
i = len - 1;
while (i >= 0 && str[i] == '0')
i--;
// Update the index of rightmost '1' in str
right_1 = i;
// If str does not contain any '1's
if (left_1 == len && right_1 == -1) {
// No changes required
cout << 0;
return;
}
// Stores minimum cost
int cost = a, zeroes;
// Iterating through str form
// left_1 to right_1
for (i = left_1; i <= right_1; i++) {
// Stores length of
// consecutive 0s
zeroes = 0;
// Calculate length of consecutive 0s
while (i < len && str[i] == '0') {
zeroes++;
i++;
}
// If a substring of 0s exists
if (zeroes)
// Update minimum cost
cost += min(zeroes * b, a);
}
// Printing the minmum cost
cout << cost;
}
// Driver Code
int main()
{
string str = "01101110";
int A = 5, B = 1;
convert_to_allzeroes(str, A, B);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find the minimum cost
// to convert given string to 0s only
public static void convert_to_allzeroes(String str,
int a, int b)
{
// Length of string
int len = str.length();
// Stores the index of leftmost '1' in str
int left_1, i = 0;
while (i < len && str.charAt(i) == '0')
i++;
// Update the index of leftmost '1' in str
left_1 = i;
// Stores the index of rightmost '1' in str
int right_1;
i = len - 1;
while (i >= 0 && str.charAt(i) == '0')
i--;
// Update the index of rightmost '1' in str
right_1 = i;
// If str does not contain any '1's
if (left_1 == len && right_1 == -1)
{
// No changes required
System.out.print(0);
return;
}
// Stores minimum cost
int cost = a, zeroes;
// Iterating through str form
// left_1 to right_1
for(i = left_1; i <= right_1; i++)
{
// Stores length of
// consecutive 0s
zeroes = 0;
// Calculate length of consecutive 0s
while (i < len && str.charAt(i) == '0')
{
zeroes++;
i++;
}
// If a substring of 0s exists
if (zeroes != 0)
// Update minimum cost
cost += Math.min(zeroes * b, a);
}
// Printing the minmum cost
System.out.print(cost);
}
// Driver code
public static void main(String[] args)
{
String str = "01101110";
int A = 5, B = 1;
convert_to_allzeroes(str, A, B);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 Program to implement
# the above approach
# Function to find the minimum
# cost to convert given string
# to 0s only
def convert_to_allzeroes(st,
a, b):
# Length of string
length = len(st)
# Stores the index of
# leftmost '1' in str
left_1 = 0
i = 0
while (i < length and
st[i] == '0'):
i += 1
# Update the index of
# leftmost '1' in str
left_1 = i
# Stores the index of
# rightmost '1' in str
right_1 = 0
i = length - 1
while (i >= 0 and
st[i] == '0'):
i -= 1
# Update the index of
# rightmost '1' in str
right_1 = i
# If str does not contain
# any '1's
if (left_1 == length and
right_1 == -1):
# No changes required
print(0)
return
# Stores minimum cost
cost = a
# Iterating through str form
# left_1 to right_1
for i in range(left_1,
right_1 + 1):
# Stores length of
# consecutive 0s
zeroes = 0
# Calculate length of
# consecutive 0s
while (i < length and
st[i] == '0'):
zeroes += 1
i += 1
# If a substring of
# 0s exists
if (zeroes):
# Update minimum cost
cost += min(zeroes * b, a)
# Printing the minmum cost
print(cost)
# Driver Code
if __name__ == "__main__":
st = "01101110"
A = 5
B = 1
convert_to_allzeroes(st, A, B)
# This code is contributed by Chitranayal
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the minimum cost
// to convert given string to 0s only
public static void convert_to_allzeroes(string str,
int a, int b)
{
// Length of string
int len = str.Length;
// Stores the index of leftmost '1' in str
int left_1, i = 0;
while (i < len && str[i] == '0')
i++;
// Update the index of leftmost '1' in str
left_1 = i;
// Stores the index of rightmost '1' in str
int right_1;
i = len - 1;
while (i >= 0 && str[i] == '0')
i--;
// Update the index of rightmost '1' in str
right_1 = i;
// If str does not contain any '1's
if (left_1 == len && right_1 == -1)
{
// No changes required
Console.Write(0);
return;
}
// Stores minimum cost
int cost = a, zeroes;
// Iterating through str form
// left_1 to right_1
for(i = left_1; i <= right_1; i++)
{
// Stores length of
// consecutive 0s
zeroes = 0;
// Calculate length of consecutive 0s
while (i < len && str[i] == '0')
{
zeroes++;
i++;
}
// If a substring of 0s exists
if (zeroes != 0)
// Update minimum cost
cost += Math.Min(zeroes * b, a);
}
// Printing the minmum cost
Console.Write(cost);
}
// Driver code
public static void Main()
{
string str = "01101110";
int A = 5, B = 1;
convert_to_allzeroes(str, A, B);
}
}
// This code is contributed by susmitakundugoaldanga
6
时间复杂度: O(N),其中N是字符串的长度。
辅助空间: O(1)