Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 46s1 days 16 hours 1 minutes 46 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 46s1 days 16 hours 1 minutes 46 seconds.

Sign in to watch this lesson
Advanced - Dynamic Programming · 4:28
To count the numbers in [A, B] with a digit property, we compute f(N), the count from 0 to N, by building the numbers digit by digit and answer f(B) - f(A - 1). A tight flag keeps the prefix level with N; once a digit drops below N's digit, the rest of the subtree is free and depends only on the digits left and a small summary (here the digit sum mod 7), so it is memoised once and shared, even across calls. We also handle leading zeros with a started flag and the edge cases A = 0 and 10^18.