For nonnegative maximum jump lengths in an array, let and let unreachable positions have value . Classifying a route to by its penultimate position proves this dynamic programming recurrence. Increasing is a valid dependency order because every jump advances. Scanning all previous positions uses time complexity and space complexity. Store a minimizing predecessor to reconstruct a shortest route.
An embedding is determined by its chosen increasing sequence of positions. Accordingly, the all-subsequence embedding kernel counts pairs of embeddings, one in and one in , that spell the same subsequence. The two embeddings are distinguished even when they spell the same word as another pair. The empty word contributes exactly one pair.
Let , , and define for prefixes. When either prefix is empty, only the empty word has an embedding in both, so
To derive a dynamic programming recurrence, count pairs that do not use position of or do not use position of . Inclusion-exclusion gives
The remaining pairs use both final positions. These positions must be the final letters of the common word, so they contribute nothing if . If , deleting those two final positions is a bijection to arbitrary embedding pairs on the two shorter prefixes, including their empty pair. Their contribution is therefore . Hence
Equivalently, when the final symbols agree the recurrence simplifies to ; when they differ, subtract the diagonal term. Fill the table in increasing prefix order and return . The boundary values and the disjoint counting argument prove correctness by induction on .
A one-row implementation of this dynamic programming uses the saved old diagonal entry:
F[0..p] := 1
for i := 1 to n:
    diagonal := 1
    for j := 1 to p:
        above := F[j]
        if x[i] = y[j]:
            F[j] := above + F[j-1]
        else:
            F[j] := above + F[j-1] - diagonal
        diagonal := above
return F[p]
The assignment to diagonal retains the value from the previous row before F[j] was overwritten. Swapping and if necessary minimizes the row length. Thus the time complexity is arithmetic operations and the extra space complexity is integer cells. Empty inputs return one immediately.
These counts include multiplicity: for , the empty word contributes , the word contributes , and contributes , giving , not three. More generally, each result is at most , since it counts some pairs of position subsets. Exact integers therefore require bits; the stated quadratic bound is an arithmetic-operation bound, with a conservative bound for elementary bit arithmetic.
The proposed positive-semidefinite kernel is valid: for each finite string only finitely many coordinates are nonzero, so is a well-defined feature map into the inner product space of finitely supported coordinate families, and its inner product is exactly .
Dynamic programming solves related subproblems once, stores their results, and combines those results in an order consistent with their dependencies. The state must retain all information that determines the remaining choices and their costs. Here an adequate dynamic programming state is the current array index, since future jumps depend only on that index and the fixed input.
The principle of optimality says that the remaining part of an optimal solution must be optimal for the residual problem defined by its state. Equivalently here, the prefix ending at a particular intermediate position of a shortest route must be a shortest route to that position: replacing it by a shorter prefix would shorten the entire route. This is the optimal substructure needed by the dynamic programming recurrence.
Thus . Overlapping routes share states, so storing results avoids repeatedly solving the same subproblem.
Let be the minimum number of stamps for total . Since denomination 1 is available, every nonnegative total is reachable. The dynamic programming recurrence is
Fill the table in increasing order of :
F[0] = 0
for x = 1,...,m:
    F[x] = infinity
    for i = 1,...,n:
        if d[i] <= x:
            F[x] = min(F[x], 1 + F[x-d[i]])
return F[m]
Every representation of has a last stamp , so its remaining count is at least . Conversely, appending to an optimal residual representation attains each candidate. Thus the recurrence is exact by the principle of optimality. Worst-case time is , with storage. This is pseudo-polynomial time in the numerical target, rather than polynomial time in its binary encoding length.
Principle of optimality 2026-10-31
After fixing an initial decision in an optimal solution, the remaining decisions must solve the resulting residual problem optimally, with the relevant state and constraints retained. Otherwise substituting a better continuation would improve the original solution. This replacement argument justifies dynamic programming recurrences when the chosen dynamic programming state contains all information needed by future decisions.