Fix distances
This commit is contained in:
parent
cf7bcd21c2
commit
5a4ac55052
|
@ -120,11 +120,11 @@ Of course, the same applies to coins 3 and 4.
|
||||||
Thus, we can use the following recursive formula
|
Thus, we can use the following recursive formula
|
||||||
to calculate the minimum number of coins:
|
to calculate the minimum number of coins:
|
||||||
\begin{equation*}
|
\begin{equation*}
|
||||||
\begin{aligned}
|
\begin{split}
|
||||||
\texttt{solve}(x) & = \min( & \texttt{solve}(x-1)+1 & , \\
|
\texttt{solve}(x) = \min( & \texttt{solve}(x-1)+1, \\
|
||||||
& & \texttt{solve}(x-3)+1 & , \\
|
& \texttt{solve}(x-3)+1, \\
|
||||||
& & \texttt{solve}(x-4)+1 & ).
|
& \texttt{solve}(x-4)+1).
|
||||||
\end{aligned}
|
\end{split}
|
||||||
\end{equation*}
|
\end{equation*}
|
||||||
The base case of the recursion is $\texttt{solve}(0)=0$,
|
The base case of the recursion is $\texttt{solve}(0)=0$,
|
||||||
because no coins are needed to form an empty sum.
|
because no coins are needed to form an empty sum.
|
||||||
|
@ -326,11 +326,11 @@ we can form the sum $x$.
|
||||||
For example, if $\texttt{coins}=\{1,3,4\}$,
|
For example, if $\texttt{coins}=\{1,3,4\}$,
|
||||||
then $\texttt{solve}(5)=6$ and the recursive formula is
|
then $\texttt{solve}(5)=6$ and the recursive formula is
|
||||||
\begin{equation*}
|
\begin{equation*}
|
||||||
\begin{aligned}
|
\begin{split}
|
||||||
\texttt{solve}(x) & = & \texttt{solve}(x-1) & + \\
|
\texttt{solve}(x) = & \texttt{solve}(x-1) + \\
|
||||||
& & \texttt{solve}(x-3) & + \\
|
& \texttt{solve}(x-3) + \\
|
||||||
& & \texttt{solve}(x-4) & .
|
& \texttt{solve}(x-4) .
|
||||||
\end{aligned}
|
\end{split}
|
||||||
\end{equation*}
|
\end{equation*}
|
||||||
|
|
||||||
In this case, the general recursive function is as follows:
|
In this case, the general recursive function is as follows:
|
||||||
|
@ -801,11 +801,11 @@ between \texttt{x} and \texttt{y} equals $\texttt{distance}(n-1,m-1)$.
|
||||||
We can calculate the values of \texttt{distance}
|
We can calculate the values of \texttt{distance}
|
||||||
as follows:
|
as follows:
|
||||||
\begin{equation*}
|
\begin{equation*}
|
||||||
\begin{aligned}
|
\begin{split}
|
||||||
\texttt{distance}(a,b) & = \min(& \texttt{distance}(a,b-1)+1 & , \\
|
\texttt{distance}(a,b) = \min(& \texttt{distance}(a,b-1)+1, \\
|
||||||
& & \texttt{distance}(a-1,b)+1 & , \\
|
& \texttt{distance}(a-1,b)+1, \\
|
||||||
& & \texttt{distance}(a-1,b-1)+\texttt{cost}(a,b) & ).
|
& \texttt{distance}(a-1,b-1)+\texttt{cost}(a,b)).
|
||||||
\end{aligned}
|
\end{split}
|
||||||
\end{equation*}
|
\end{equation*}
|
||||||
Here $\texttt{cost}(a,b)=0$ if $\texttt{x}[a]=\texttt{y}[b]$,
|
Here $\texttt{cost}(a,b)=0$ if $\texttt{x}[a]=\texttt{y}[b]$,
|
||||||
and otherwise $\texttt{cost}(a,b)=1$.
|
and otherwise $\texttt{cost}(a,b)=1$.
|
||||||
|
|
Loading…
Reference in New Issue