dp
of size k x n
and a 2D array p
of the same size, where k
is the length of targetPath
and n
is the number of cities.dp[0][v]
for each vertex v
based on whether the name of v
matches targetPath[0]
.i
from 1
to k-1
, iterate over all edges (u, v)
and update dp[i][v]
based on the minimum edit distance from dp[i-1][u]
plus a potential mismatch if names[v]
does not equal targetPath[i]
.p[i][v]
with the vertex u
that led to the minimum dp[i][v]
.v
that minimizes dp[k-1][v]
to identify the last vertex of the optimal path.p[k-1][v]
to p[0][v]
and reverse it to get the final answer.