- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimize cost to convert all characters of a binary string to 0s
A binary string is a string that only contains the binary numbers in it. In this problem, we are given a binary string, an array that represents the last index up to which we can flip the ones after starting from the ith index which will cost and cost for each index is given in another cost array. We have to perform some number of operations on the string to make the string completely zero.
Example
Let's understand the problem with the help of an example −
Input
string str = "101011" int arr[] = {1, 2, 2, 4, 5, 5}; int cost[] = {3, 9, 2, 3, 7, 2}
Output
19
Explanation
Here we must have to flip the first bit, so the cost will be 3 and the next 0 will also be flipped, then we have to flip that too and cost will be 3 + 9 that is 12. Again next 1 is flipped 1 time only so no need to flip it. Next zero is not flipped here, so again no need to flip it.
Then we need to flip the second last 1 and it will also flip the next 1 with total cost 7 and overall cost of 19.
Approach 1
In this approach, we are going to use the priority queue to indicate up to which index we have to find the impact of the flip.
Example
#include <bits/stdc++.h> using namespace std; // function to get the required cost int costReq(string s, int size, int arr[], int cost[]){ // store the elements in the priority queue all the elements will be stored in the smallest first side priority_queue<int, vector<int>, greater<int>> pq; // variable to store the number of times the string is flipped int countFlips = 0; // variable to store the required minimum cost or to store the answer int ans = 0; // using for loop traversing over the string for(int i = 0; i < size; i++){ // remove all the values from the priority queue which have value less than current index while(pq.empty() == false && pq.top() < i){ // poping the elements pq.pop(); countFlips--; // reducing the count of the flips } // updating the value of the current character based on the number of times it have been flipped if(countFlips & 1){ // if the count of the flips is odd then change the current index if(s[i] == '1'){ s[i] = '0'; } else { s[i] = '1'; } } // after updation if the current index is non-zero then we need to flip it if (s[i] == '1'){ // update the count of the flips countFlips++; // add number of index upto which we are flipping pq.push(arr[i]); // add cost to answer ans += cost[i]; } } return ans; // return the final answer } int main(){ string str = "101011"; // given string int arr[] = {1, 2, 2, 4, 5, 5}; // given index array int cost[] = {3, 9, 2, 3, 7, 2}; // given cost array int n = str.length(); // getting length of the string // calling to the function to get the answer cout << "The minimum cost required to flip all the ones to zeros is: "<<costReq(str, n, arr, cost)<<endl; return 0; }
Output
The minimum cost required to flip all the ones to zeros is: 19
Time and Space Complexity
The time complexity of the above code is O(N*log(N)) where N is the size of the given string, we are getting the logarithmic factor due to the priority queue.
The space complexity of the above code is O(N), because we are using the extra space as priority queue.
Approach 2
In this approach, we are going to implement the code by using the difference array.
Example
#include <bits/stdc++.h> using namespace std; // function to get the required cost int costReq(string s, int size, int arr[], int cost[]){ // vector to work as the difference array vector<int> diff_arr(size + 1); // variable to store the number of times the string is flipped int countFlips = 0; // variable to store the required minimum cost or to store the answer int ans = 0; // using for loop traversing over the string for(int i = 0; i < size; i++){ // updating the value of the flips using the differ rence array countFlips += diff_arr[i]; // updating the value of the current character based on the number of times it has been flipped if(countFlips & 1){ // if the count of the flips is odd then change the current index if(s[i] == '1'){ s[i] = '0'; } else { s[i] = '1'; } } // after updation if the current index is non-zero then we need to flip it if (s[i] == '1'){ // update the count of the flips countFlips++; // update the value of the difference array on the next index of the ending diff_arr[arr[i] + 1]--; // add cost to answer ans += cost[i]; } } return ans; // return the final answer } int main(){ string str = "101011"; // given string int arr[] = {1, 2, 2, 4, 5, 5}; // given index array int cost[] = {3, 9, 2, 3, 7, 2}; // given cost array int n = str.length(); // getting lenth of the string // calling to the function to get the answer cout << "The minimum cost required to flip all the ones to zeros is: "<<costReq(str, n, arr, cost)<<endl; return 0; }
Output
The minimum cost required to flip all the ones to zeros is: 19
Time and Space Complexity
The time complexity of the above code is O(N), because we are just traversing over the string and maintaining an array.
The space complexity of the above code is O(N), as we are maintaining the difference array.
Conclusion
In this tutorial, we have implemented a program to find the minimum cost to convert a given binary string completely of zeros. We are given an array in which each index represents the number of indexes forward elements are flipped if we flip this and cost is also given. We have implemented two approaches, one with priority queue and O(N*log(N)) time complexity while the another with difference array and O(N) time complexity.