Given a binary string S, and two integers A, which denotes the cost of reversing a substring, and B, which denotes the cost of flipping all characters of a substring, the task is to find the minimum cost to reduce the string S to 1s only.
Examples:
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
Approach: The given problem can be solved based on the following observations:
- 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.
Follow the steps below to solve the problem:
- Initialize a variable say P with 0 value to store the count of disjoint groups of continuous 0s.
- Also, initialize a variable say count as 0 to store the temporary count of the number of 0s in a group.
- Iterate over the character of the string S and do the following:
- If the current character is ‘0‘ then increment the count by 1.
- Otherwise, if the count is greater than 0 then increment P by 1 and then assign 0 to count.
- If the count is greater than 0 then increment P by 1.
- Print the minimum cost obtained as (P-1)*A+B.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void MinimumCost(string S, int A, int B)
{
int count = 0;
int group = 0;
for ( int i = 0; i < S.size(); i++) {
if (S[i] == '0' ) {
count += 1;
}
else {
if (count > 0) {
group += 1;
}
count = 0;
}
}
if (count > 0)
group += 1;
if (group == 0) {
cout << 0 << endl;
}
else {
cout << min(A, B) * (group - 1) + B;
}
}
int main()
{
int A = 1;
int B = 5;
string S = "01100" ;
MinimumCost(S, A, B);
return 0;
}
|
Java
class GFG{
static void MinimumCost(String S, int A, int B)
{
int count = 0 ;
int group = 0 ;
for ( int i = 0 ; i < S.length(); i++)
{
if (S.charAt(i) == '0' )
{
count += 1 ;
}
else
{
if (count > 0 )
{
group += 1 ;
}
count = 0 ;
}
}
if (count > 0 )
group += 1 ;
if (group == 0 )
{
System.out.println( 0 );
}
else
{
System.out.print(Math.min(A, B) *
(group - 1 ) + B);
}
}
public static void main(String args[])
{
int A = 1 ;
int B = 5 ;
String S = "01100" ;
MinimumCost(S, A, B);
}
}
|
Python3
def MinimumCost(S, A, B):
count = 0
group = 0
for i in range ( len (S)):
if (S[i] = = '0' ):
count + = 1
else :
if (count > 0 ):
group + = 1
count = 0
if (count > 0 ):
group + = 1
if (group = = 0 ):
print ( 0 )
else :
print ( min (A, B) * (group - 1 ) + B)
if __name__ = = '__main__' :
A = 1
B = 5
S = "01100"
MinimumCost(S, A, B)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void MinimumCost( string S, int A, int B)
{
int count = 0;
int group = 0;
for ( int i = 0; i < S.Length; i++)
{
if (S[i] == '0' )
{
count += 1;
}
else
{
if (count > 0)
{
group += 1;
}
count = 0;
}
}
if (count > 0)
group += 1;
if ( group == 0)
{
Console.WriteLine(0);
}
else
{
Console.Write(Math.Min(A, B) *
( group - 1) + B);
}
}
public static void Main()
{
int A = 1;
int B = 5;
string S = "01100" ;
MinimumCost(S, A, B);
}
}
|
Javascript
<script>
function MinimumCost(S, A, B) {
let count = 0;
let group = 0;
for (let i = 0; i < S.length; i++) {
if (S[i] == '0' ) {
count += 1;
}
else
{
if (count > 0) {
group += 1;
}
count = 0;
}
}
if (count > 0)
group += 1;
if (group == 0) {
document.write(0 + "<br>" );
}
else {
document.write(Math.min(A, B) * (group - 1) + B);
}
}
let A = 1;
let B = 5;
let S = "01100" ;
MinimumCost(S, A, B);
</script>
|
Time Complexity: O(N), The time complexity is O(n), where n is the length of the input string S. This is because the program traverses the entire string once to count the number of groups of consecutive 0’s, and then performs a constant number of operations to compute and output the minimum cost. Therefore, the time complexity is linear in the length of the input string.
Auxiliary Space: O(1), The space complexity of the program is O(1), which is constant. This is because the program only uses a fixed number of integer variables to store the count and group information, and a constant amount of space is used for the input string and any other internal data structures created by the program. Thus, the space used by the program does not depend on the size of the input, and is constant irrespective of the length of the input string.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 May, 2023
Like Article
Save Article