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 of 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++
// C++ program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to get the minimum// Cost to convert all characters// of given string to 0sint minCost(string str, int N, int R[], int C[]){ // Stores the range of indexes // of characters that need // to be flipped priority_queue<int, vector<int>, greater<int> > pq; // Stores the number of times // current character is flipped int flip = 0; // Stores minimum cost to get // the required string int cost = 0; // Traverse the given string for (int i = 0; i < N; i++) { // Remove all value from pq // whose value is less than i while (pq.size() > 0 and pq.top() < i) { pq.pop(); // Update flip flip--; } // If current character // is flipped odd times if (flip % 2 == 1) { str[i] = '1' - str[i] + '0'; } // If current character contains // non-zero value if (str[i] == '1') { // Update flip flip++; // Update cost cost += C[i]; // Append R[i] into pq pq.push(R[i]); } } return cost;}// Driver Codeint main(){ string str = "1010"; int R[] = { 1, 2, 2, 3 }; int C[] = { 3, 1, 2, 3 }; int N = str.length(); // Function call cout << minCost(str, N, R, C);} |
Java
// Java program to implement// the above approachimport java.util.*;import java.lang.*;import java.io.*;class GFG { // Function to get the minimum // Cost to convert all characters // of given string to 0s public static int minCost(String s, int R[], int C[], int N) { char ch[] = s.toCharArray(); // Stores the range of indexes // of characters that need // to be flipped PriorityQueue<Integer> pq = new PriorityQueue<>(); // Stores the number of times // current character is flipped int flip = 0; // Stores minimum cost to get // the required string int cost = 0; // Traverse the given string for (int i = 0; i < N; i++) { // Remove all value from pq // whose value is less than i while (pq.size() > 0 && pq.peek() < i) { pq.poll(); // Update flip flip--; } // Get the current number int cn = ch[i] - '0'; // If current character // is flipped odd times if (flip % 2 == 1) cn = 1 - cn; // If current character contains // non-zero value if (cn == 1) { // Update flip flip++; // Update cost cost += C[i]; // Append R[i] into pq pq.add(R[i]); } } return cost; } // Driver Code public static void main(String[] args) { int N = 4; String s = "1010"; int R[] = { 1, 2, 2, 3 }; int C[] = { 3, 1, 2, 3 }; // Function call System.out.println(minCost(s, R, C, N)); }} |
C#
// C# program to implement// the above approachusing System;using System.Collections.Generic;class GFG{// Function to get the minimum// Cost to convert all characters// of given string to 0spublic static int minCost(String s, int []R, int []C, int N){ char []ch = s.ToCharArray(); // Stores the range of indexes // of characters that need // to be flipped Queue<int> pq = new Queue<int>(); // Stores the number of times // current character is flipped int flip = 0; // Stores minimum cost to get // the required string int cost = 0; // Traverse the given string for(int i = 0; i < N; i++) { // Remove all value from pq // whose value is less than i while (pq.Count > 0 && pq.Peek() < i) { pq.Dequeue(); // Update flip flip--; } // Get the current number int cn = ch[i] - '0'; // If current character // is flipped odd times if (flip % 2 == 1) cn = 1 - cn; // If current character contains // non-zero value if (cn == 1) { // Update flip flip++; // Update cost cost += C[i]; // Append R[i] into pq pq.Enqueue(R[i]); } } return cost;}// Driver Codepublic static void Main(String[] args){ int N = 4; String s = "1010"; int []R = { 1, 2, 2, 3 }; int []C = { 3, 1, 2, 3 }; // Function call Console.WriteLine(minCost(s, R, C, N));}}// This code is contributed by Amit Katiyar |
4
Time Complexity: O(N * log N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

