通过反转或翻转子字符串的字符来最小化使二进制字符串的所有字符等于“1”的成本
给定一个二进制字符串S和两个整数A ,表示反转子字符串的成本,和B ,表示翻转子字符串的所有字符的成本,任务是找到将字符串S减少到1的最小成本仅限s。
例子:
Input: S = “01100”, A = 1, B = 5
Output: 6
Explanation:
One possible way to make all characters equal to ‘1’ is as follows:
- Reverse the substring {S[0], S[2]}. Cost = A (= 1), The string modifies to “11000”.
- Flip the characters of substring {S[2], S[4]}. Cost of B (= 5). The string modifies to “11111”.
Therefore, the total cost = 5+1 = 6 (which is the minimum cost possible)
Input: S = “1111111”, A = 2, B = 3
Output: 0
方法:可以根据以下观察解决给定的问题:
- Assuming there are P disjoint groups of continuous ‘0’s,
- If A is less than B, then it is optimal to convert P groups into ‘1’ group of continuous ‘0’s by performing the first type of operation for a cost of (P – 1) * A and then converting all the ‘0’s to ‘1’s for a cost of B.
- Otherwise, it is optimal to perform the second operation on each group separately, for a cost of B * P.
请按照以下步骤解决问题:
- 用0值初始化一个变量P以存储不相交的连续0组的计数。
- 另外,初始化一个变量 say count为0以存储组中0的数量的临时计数。
- 遍历字符串S的字符并执行以下操作:
- 如果当前字符为“ 0 ”,则将计数增加1 。
- 否则,如果计数大于0 ,则将P增加1 ,然后将0分配给count 。
- 如果计数大于0 ,则将P加1。
- 将获得的最小成本打印为(P-1)*A+B。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to calculate minimum cost to
// convert all the characters of S to '1'
void MinimumCost(string S, int A, int B)
{
// Stores the result
int count = 0;
// Stores the number of groups
// that have 0 as characters
int group = 0;
// Traverse the string S
for (int i = 0; i < S.size(); i++) {
// If current character is '0'
if (S[i] == '0') {
count += 1;
}
else {
// If count is greater
// than 0
if (count > 0) {
group += 1;
}
// Set the count to 0
count = 0;
}
}
// If the last few consecutive
// characters are '0'
if (count > 0)
group += 1;
// If string contains
// all characters as '1'
if (group == 0) {
cout << 0 << endl;
}
else {
// Minimum Cost
cout << min(A, B) * (group - 1) + B;
}
}
// Driver Code
int main()
{
// Given Input
int A = 1;
int B = 5;
string S = "01100";
// Function Call
MinimumCost(S, A, B);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to calculate minimum cost to
// convert all the characters of S to '1'
static void MinimumCost(String S, int A, int B)
{
// Stores the result
int count = 0;
// Stores the number of groups
// that have 0 as characters
int group = 0;
// Traverse the string S
for(int i = 0; i < S.length(); i++)
{
// If current character is '0'
if (S.charAt(i) == '0')
{
count += 1;
}
else
{
// If count is greater
// than 0
if (count > 0)
{
group += 1;
}
// Set the count to 0
count = 0;
}
}
// If the last few consecutive
// characters are '0'
if (count > 0)
group += 1;
// If string contains
// all characters as '1'
if (group == 0)
{
System.out.println(0);
}
else
{
// Minimum Cost
System.out.print(Math.min(A, B) *
(group - 1) + B);
}
}
// Driver Code
public static void main(String args[])
{
// Given Input
int A = 1;
int B = 5;
String S = "01100";
// Function Call
MinimumCost(S, A, B);
}
}
// This code is contributed by SoumikMondal
Python3
# Python3 program for the above approach
# Function to calculate minimum cost to
# convert all the characters of S to '1'
def MinimumCost(S, A, B):
# Stores the result
count = 0
# Stores the number of groups
# that have 0 as characters
group = 0
# Traverse the S
for i in range(len(S)):
# If current character is '0'
if (S[i] == '0'):
count += 1
else:
# If count is greater
# than 0
if (count > 0):
group += 1
# Set the count to 0
count = 0
# If the last few consecutive
# characters are '0'
if (count > 0):
group += 1
# If contains
# all characters as '1'
if (group == 0):
print(0)
else:
# Minimum Cost
print(min(A, B) * (group - 1) + B)
# Driver Code
if __name__ == '__main__':
# Given Input
A = 1
B = 5
S = "01100"
# Function Call
MinimumCost(S, A, B)
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to calculate minimum cost to
// convert all the characters of S to '1'
static void MinimumCost(string S, int A, int B)
{
// Stores the result
int count = 0;
// Stores the number of groups
// that have 0 as characters
int group = 0;
// Traverse the string S
for(int i = 0; i < S.Length; i++)
{
// If current character is '0'
if (S[i] == '0')
{
count += 1;
}
else
{
// If count is greater
// than 0
if (count > 0)
{
group += 1;
}
// Set the count to 0
count = 0;
}
}
// If the last few consecutive
// characters are '0'
if (count > 0)
group += 1;
// If string contains
// all characters as '1'
if (group == 0)
{
Console.WriteLine(0);
}
else
{
// Minimum Cost
Console.Write(Math.Min(A, B) *
(group - 1) + B);
}
}
// Driver Code
public static void Main()
{
// Given Input
int A = 1;
int B = 5;
string S = "01100";
// Function Call
MinimumCost(S, A, B);
}
}
// This code is contributed by SURENDRA_GANGWAR
Javascript
输出:
6
时间复杂度: O(N)
辅助空间: O(1)