Overview
Page for any competitive programming algorithms and techniques I learn.
Edit Distance Revisited (1/10/2026)
A reminder, that for any dynamic programming problem, is that we can think of it as a mathematical optimisation problem, which is defined by
- Overlapping Subproblems
- Optimal Substructure
We think about the recurrence that links different states, and define a state space that’s helpful for our problem. In the case of edit distance, for strings and , we defined the state as the minimum edit distance between the prefixes and . That way, our solution is simply .
We define the recurrence as:
Here, is if the characters are the same, and otherwise. If you think about the recurrence, cover the character at position and in and respectively. We can get to that point via insertion , deletion , or substituting a character .
Trees
To motivate the many ways we can solve LCA, we start with a naive implementation.
Naive LCA
Binary Lifting
Think of this as an amazing teleportation technique. If we have the ability to ascend levels in one jump, then we can reach any ancestor in jumps. After which, we can then trivially iterate on the remaining ones. As an example, if we want to go up levels, we can do jumps, which is jumps total. We look at the first ancestor of our node, then the ancestor of that node, and then the ancestor of that node.
All of this corresponds to looking at a table repeatedly until we get to our destination.
Resources
- Resource 1
- Resource 2