Given a binary string str and an integer K, the task is to find the minimum cost required to partition the string into exactly K segments when the cost of each segment is the product of the number of set bits with the number of unset bits and total cost is sum of cost of all the individual segments.
Input: str = “110101”, K = 3
11|0|101 is one of the possible partitions
where the cost is 0 + 0 + 2 = 2
Input: str = “1000000”, K = 5
Approach: Write a function minCost(s, k, cost, i, n) where cost is the minimum cost so far, i is the starting index for the partition and k is the remaining segments to be partitioned. Now, starting from the ith index calculate the cost of the current partition and call the same function recursively for the remaining substring. To memoize the result, a dp array will be used where dp[i][j] will store the minimum cost of partitioning the string into j parts starting at the ith index.
Below is the implementation of the above approach:
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- Minimum cost to make a string free of a subsequence
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum steps to remove substring 010 from a binary string
- Minimum steps to convert one binary string to other only using negation
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of operations required to sum to binary string S
- Minimum swaps required to convert one binary string to another
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum number of swaps to make two binary string equal
- Minimum swaps required to make a binary string alternating
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum number of palindromic subsequences to be removed to empty a binary string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.