Problem solution · C++

Minimum Cost to Partition a Binary String

Minimum Cost to Partition a Binary String: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Dynamic programming
Source
Kamyu LeetCode Solutions
Length
55 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Minimum Cost to Partition a Binary String, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 55 lines of C++ from the credited upstream file minimum-cost-to-partition-a-binary-string.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 6 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMinimum Cost to Partition a Binary String · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n)// Space: O(n) // prefix sum, divide and conquerclass Solution {public:    long long minCost(string s, int encCost, int flatCost) {        vector<int64_t> prefix(size(s) + 1);        const auto divide_and_conquer = [&](this auto&& divide_and_conquer, int left, int right) -> int64_t {            const auto& l = right - left + 1;            const auto& x = prefix[right + 1] - prefix[left];            int64_t result = x ? l * x * encCost : flatCost;            if (x && l % 2 == 0) {                result = min(result, divide_and_conquer(left, (left + l / 2) - 1) + divide_and_conquer(left + l / 2, right));            }            return result;        };         for (int i = 0; i < size(s); ++i) {            prefix[i + 1] = prefix[i] + (s[i] == '1' ? 1 : 0);        }        return divide_and_conquer(0, size(s) - 1);    }}; // Time:  O(n)// Space: O(n)// dpclass Solution2 {public:    long long minCost(string s, int encCost, int flatCost) {        int l = size(s);        for (; l % 2 == 0; l >>= 1);        vector<pair<int64_t, int64_t>> dp;        for (int left = 0; left < size(s); left += l) {            int64_t x = 0;            for (int i = left; i < left + l; ++i) {                x += (s[i] == '1') ? 1 : 0;            }            dp.emplace_back(x ? l * x * encCost : flatCost, x);        }        while (size(dp) != 1) {            vector<pair<int64_t, int64_t>> new_dp;            l <<= 1;            for (int i = 0; i < size(dp); i += 2) {                const auto& v = dp[i].first + dp[i + 1].first;                const auto& x = dp[i].second + dp[i + 1].second;                new_dp.emplace_back(x ? min(l * x * encCost, v) : flatCost, x);            }            dp = move(new_dp);        }        return dp[0].first;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗