Minimize cost to convert all characters of a binary string to 0s
Given a binary string, str, two integer arrays R[], and C[] of size N. Flipping all the characters from index i to R[i] requires C[i] cost. The task is to minimize the cost required to convert the given binary string to only 0s.
Examples:
Input: str = “1010”, R[] = {1, 2, 2, 3}, C[] = {3, 1, 2, 3}
Output: 4
Explanation:
Flipping all the characters from index 1 to 2, modifies str to “1100”. Therefore, cost = 1
Flipping all the characters from index 1 to 2, modifies str to “0000”. Therefore, cost = cost + 3 = 4
Therefore, minimum cost required is 4.
Input: str = “01100”, R[] = {1, 2, 3, 4, 5}, C[] = {1, 5, 5, 2, 3}
Output: 10
Approach: The problem can be solved using Greedy technique. The idea is to traverse the given string from left to right and check if the current character is a non-zero character or not. If found to be true, then flip all the characters from the current index (= i) to R[i]th index. Follow the steps below to solve the problem:
- Initialize a variable, say flip, to store the number of times current character can be flipped.
- Create a priority queue, say pq to store the range of indexes of characters on the right side of current character whose flip value is greater than 0.
- Initialize a variable, say cost to store the minimum cost to obtain the required string.
- Traverse the given string and check if the current character is ‘1‘ or not. If found to be true, then flip all the characters in the range i to R[i] and increment the value of cost by C[i].
- Finally, print the value of cost.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost(string str, int N, int R[], int C[])
{
priority_queue< int , vector< int >, greater< int > > pq;
int flip = 0;
int cost = 0;
for ( int i = 0; i < N; i++)
{
while (pq.size() > 0 and pq.top() < i)
{
pq.pop();
flip--;
}
if (flip % 2 == 1)
{
str[i] = '1' - str[i] + '0' ;
}
if (str[i] == '1' )
{
flip++;
cost += C[i];
pq.push(R[i]);
}
}
return cost;
}
int main()
{
string str = "1010" ;
int R[] = { 1, 2, 2, 3 };
int C[] = { 3, 1, 2, 3 };
int N = str.length();
cout << minCost(str, N, R, C);
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static int minCost(String s,
int R[],
int C[],
int N)
{
char ch[] = s.toCharArray();
PriorityQueue<Integer> pq = new PriorityQueue<>();
int flip = 0 ;
int cost = 0 ;
for ( int i = 0 ; i < N; i++) {
while (pq.size() > 0 && pq.peek() < i)
{
pq.poll();
flip--;
}
int cn = ch[i] - '0' ;
if (flip % 2 == 1 )
cn = 1 - cn;
if (cn == 1 )
{
flip++;
cost += C[i];
pq.add(R[i]);
}
}
return cost;
}
public static void main(String[] args)
{
int N = 4 ;
String s = "1010" ;
int R[] = { 1 , 2 , 2 , 3 };
int C[] = { 3 , 1 , 2 , 3 };
System.out.println(minCost(s, R, C, N));
}
}
|
Python3
def minCost(s, R, C, N) :
ch = list (s)
pq = []
flip = 0
cost = 0
for i in range (N) :
while ( len (pq) > 0 and pq[ 0 ] < i) :
pq.pop( 0 );
flip - = 1
cn = ord (ch[i]) - ord ( '0' )
if (flip % 2 = = 1 ) :
cn = 1 - cn
if (cn = = 1 ) :
flip + = 1
cost + = C[i]
pq.append(R[i])
return cost
N = 4
s = "1010"
R = [ 1 , 2 , 2 , 3 ]
C = [ 3 , 1 , 2 , 3 ]
print (minCost(s, R, C, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int minCost(String s, int []R,
int []C, int N)
{
char []ch = s.ToCharArray();
Queue< int > pq = new Queue< int >();
int flip = 0;
int cost = 0;
for ( int i = 0; i < N; i++)
{
while (pq.Count > 0 && pq.Peek() < i)
{
pq.Dequeue();
flip--;
}
int cn = ch[i] - '0' ;
if (flip % 2 == 1)
cn = 1 - cn;
if (cn == 1)
{
flip++;
cost += C[i];
pq.Enqueue(R[i]);
}
}
return cost;
}
public static void Main(String[] args)
{
int N = 4;
String s = "1010" ;
int []R = { 1, 2, 2, 3 };
int []C = { 3, 1, 2, 3 };
Console.WriteLine(minCost(s, R, C, N));
}
}
|
Javascript
<script>
function minCost(str, N, R, C)
{
str = str.split( '' );
var pq = [];
var flip = 0;
var cost = 0;
for ( var i = 0; i < N; i++)
{
while (pq.length > 0 && pq[pq.length-1] < i)
{
pq.pop();
flip--;
}
if (flip % 2 == 1)
{
str[i] = String.fromCharCode( '1' .charCodeAt(0) - str[i].charCodeAt(0) + '0' .charCodeAt(0));
}
if (str[i] == '1' )
{
flip++;
cost += C[i];
pq.push(R[i]);
}
pq.sort((a,b)=>b-a);
}
return cost;
}
var str = "1010" ;
var R = [1, 2, 2, 3 ];
var C = [3, 1, 2, 3 ];
var N = str.length;
document.write(minCost(str, N, R, C));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Last Updated :
29 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...