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:
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.
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- Minimum cost to convert given string to consist of only vowels
- Minimum cost to make a string free of a subsequence
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Minimum flips required to keep all 1s together in a Binary string
- Minimum swaps required to convert one binary string to another
- Minimum steps to remove substring 010 from a binary string
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum steps to convert one binary string to other only using negation
- Minimum number of operations required to sum to binary string S
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum number of swaps to make two binary string equal
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum swaps required to make a binary string alternating
- Minimum number of operations required to obtain a given Binary String
- Minimum swaps required to make a binary string divisible by 2^k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.