Problem solution · C++

Number of Balanced Integers in a Range

Number of Balanced Integers in a Range: 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
113 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 Number of Balanced Integers in a Range, 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

  • 113 lines of C++ from the credited upstream file number-of-balanced-integers-in-a-range.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 9 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 codeNumber of Balanced Integers in a Range · C++C++
Use this to learn the idea, then write your own version.
// Time:  O((logn)^2)// Space: O(logn) // dpclass Solution {public:    long long countBalanced(long long low, long long high) {        const auto& count = [&](int64_t n) {            vector<int> digits;            for (; n; n /= 10) {                digits.emplace_back(n % 10);            }            ranges::reverse(digits);            const auto& shift = size(digits) / 2 * 9;            vector<vector<int64_t>> dp(size(digits) * 9 + 1, vector<int64_t>(2));            dp[shift][1] = 1;            for (int i = 0; i < size(digits); ++i) {                vector<vector<int64_t>> new_dp(size(digits) * 9 + 1, vector<int64_t>(2));                for (int curr = 0; curr < size(dp); ++curr) {                    for (int tight = 0; tight <= 1; ++tight) {                        if (dp[curr][tight] == 0) {                            continue;                        }                                                for (int d = 0, bound = tight ? digits[i] : 9; d <= bound; ++d) {                            new_dp[(i & 1) ? curr - d : curr + d][tight && d == bound] += dp[curr][tight];                        }                    }                }                dp = move(new_dp);            }                        return dp[shift][0];        };                return count(high + 1) - count(low);    }}; // Time:  O((logn)^2)// Space: O((logn)^2)// memoizationclass Solution2 {public:    long long countBalanced(long long low, long long high) {        const auto& count = [&](int64_t n) {            vector<int> digits;            for (; n; n /= 10) {                digits.emplace_back(n % 10);            }            ranges::reverse(digits);            vector<vector<int64_t>> memo(size(digits), vector<int64_t>(size(digits) * 9 + 1, -1));            const int shift = size(digits) / 2 * 9;            const auto memoization = [&](this auto&& memoization, int i, int curr, bool tight) -> int64_t {                if (i == size(digits)) {                    return curr == shift;                }                if (!tight && memo[i][curr] != -1) {                    return memo[i][curr];                }                const auto& bound = tight ? digits[i] : 9;                int64_t result = 0;                for (int d = 0; d <= bound; ++d) {                    result += memoization(i + 1, (i & 1) ? curr - d : curr + d, tight && d == bound);                }                if (!tight) {                    memo[i][curr] = result;                }                return result;            };                        return memoization(0, shift, true);        };         return count(high) - count(low - 1);    }}; // Time:  O((logn)^2)// Space: O((logn)^2)// memoizationclass Solution3 {public:    long long countBalanced(long long low, long long high) {        const auto& count = [&](int64_t n) {            vector<int> digits;            for (; n; n /= 10) {                digits.emplace_back(n % 10);            }            ranges::reverse(digits);            vector<vector<vector<int64_t>>> memo(size(digits), vector<vector<int64_t>>(size(digits) * 9 + 1, vector<int64_t>(2, -1)));            const int shift = size(digits) / 2 * 9;            const auto memoization = [&](this auto&& memoization, int i, int curr, bool tight) -> int64_t {                if (i == size(digits)) {                    return curr == shift;                }                if (memo[i][curr][tight] == -1) {                    const auto& bound = tight ? digits[i] : 9;                    int64_t result = 0;                    for (int d = 0; d <= bound; ++d) {                        result += memoization(i + 1, (i & 1) ? curr - d : curr + d, tight && d == bound);                    }                    memo[i][curr][tight] = result;                }                return memo[i][curr][tight];            };                        return memoization(0, shift, true);        };         return count(high) - count(low - 1);    }}; 

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 ↗