Prefix Tree
Last updated
Was this helpful?
Last updated
Was this helpful?
countSteps
countSteps(n, prefix1, prefix2)
counts numbers starting with prefix1 up to but not including prefix2, bounded by n
.
It sums counts at every level of the prefix tree by multiplying prefix1 and prefix2 by 10 each time to move to the next level (deeper digits).
Summary:
The algorithm efficiently skips entire subtrees of the prefix tree based on step
counts.
Moves horizontally (curr++
) when the entire subtree is before k-th number.
Moves vertically (curr *= 10
) to dive deeper into the prefix tree when the k-th number lies in that subtree.