Count Stepping Numbers in Range - Digit DP [JS]
Description Solution: Digit DP Memoize each dp(i, prev, stateLow, stateHigh) , where i = the number of digits we currently have prev = the previous character in the sequence stateLow = whether the current number is tracking smaller, the same, or greater than low ( 0 = smaller, 1 = same, 2 = greater ) stateHigh = whether the current number is tracking smaller, the same, or greater than high ( 0 = smaller, 1 = same, 2 = greater ) For each dp(i, prev, stateLow, stateHigh) , we have two possible next digits: [prev - 1,prev + 1] . Calculate and return the total combinations. n = length of high Time Complexity: O(n * 90) Space Complexity: O(n * 90) var countSteppingNumbers = function ( low, high ) { let n = high.length, memo = Array (n).fill( 0 ).map( () => Array ( 10 ).fill( 0 ).map( () => Array ( 3 ).fill( 0 ).map( () => Array ( 3 ).fill(- 1 )))); // [n][10][3][3] let ans = 0 , MOD = 10 ...