- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- 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.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 long long countBalanced(long long low, long long high) {8 const auto& count = [&](int64_t n) {9 vector<int> digits;10 for (; n; n /= 10) {11 digits.emplace_back(n % 10);12 }13 ranges::reverse(digits);14 const auto& shift = size(digits) / 2 * 9;15 vector<vector<int64_t>> dp(size(digits) * 9 + 1, vector<int64_t>(2));16 dp[shift][1] = 1;17 for (int i = 0; i < size(digits); ++i) {18 vector<vector<int64_t>> new_dp(size(digits) * 9 + 1, vector<int64_t>(2));19 for (int curr = 0; curr < size(dp); ++curr) {20 for (int tight = 0; tight <= 1; ++tight) {21 if (dp[curr][tight] == 0) {22 continue;23 } 24 for (int d = 0, bound = tight ? digits[i] : 9; d <= bound; ++d) {25 new_dp[(i & 1) ? curr - d : curr + d][tight && d == bound] += dp[curr][tight];26 }27 }28 }29 dp = move(new_dp);30 }31 32 return dp[shift][0];33 };34 35 return count(high + 1) - count(low);36 }37};38 39404142class Solution2 {43public:44 long long countBalanced(long long low, long long high) {45 const auto& count = [&](int64_t n) {46 vector<int> digits;47 for (; n; n /= 10) {48 digits.emplace_back(n % 10);49 }50 ranges::reverse(digits);51 vector<vector<int64_t>> memo(size(digits), vector<int64_t>(size(digits) * 9 + 1, -1));52 const int shift = size(digits) / 2 * 9;53 const auto memoization = [&](this auto&& memoization, int i, int curr, bool tight) -> int64_t {54 if (i == size(digits)) {55 return curr == shift;56 }57 if (!tight && memo[i][curr] != -1) {58 return memo[i][curr];59 }60 const auto& bound = tight ? digits[i] : 9;61 int64_t result = 0;62 for (int d = 0; d <= bound; ++d) {63 result += memoization(i + 1, (i & 1) ? curr - d : curr + d, tight && d == bound);64 }65 if (!tight) {66 memo[i][curr] = result;67 }68 return result;69 };70 71 return memoization(0, shift, true);72 };73 74 return count(high) - count(low - 1);75 }76};77 78798081class Solution3 {82public:83 long long countBalanced(long long low, long long high) {84 const auto& count = [&](int64_t n) {85 vector<int> digits;86 for (; n; n /= 10) {87 digits.emplace_back(n % 10);88 }89 ranges::reverse(digits);90 vector<vector<vector<int64_t>>> memo(size(digits), vector<vector<int64_t>>(size(digits) * 9 + 1, vector<int64_t>(2, -1)));91 const int shift = size(digits) / 2 * 9;92 const auto memoization = [&](this auto&& memoization, int i, int curr, bool tight) -> int64_t {93 if (i == size(digits)) {94 return curr == shift;95 }96 if (memo[i][curr][tight] == -1) {97 const auto& bound = tight ? digits[i] : 9;98 int64_t result = 0;99 for (int d = 0; d <= bound; ++d) {100 result += memoization(i + 1, (i & 1) ? curr - d : curr + d, tight && d == bound);101 }102 memo[i][curr][tight] = result;103 }104 return memo[i][curr][tight];105 };106 107 return memoization(0, shift, true);108 };109 110 return count(high) - count(low - 1);111 }112};113