Fix indices and grammar
This commit is contained in:
parent
f551b3b422
commit
a8e456ce15
|
@ -122,7 +122,7 @@ Thus, the recursive formula is
|
||||||
where the function $\min$ gives the smallest
|
where the function $\min$ gives the smallest
|
||||||
of its parameters.
|
of its parameters.
|
||||||
In the general case, for the coin set
|
In the general case, for the coin set
|
||||||
$\{c_0,c_1,\ldots,c_{k-1}\}$,
|
$\{c_1,c_2,\ldots,c_k\}$,
|
||||||
the recursive formula is
|
the recursive formula is
|
||||||
\[f(x) = \min(f(x-c_1),f(x-c_2),\ldots,f(x-c_k))+1.\]
|
\[f(x) = \min(f(x-c_1),f(x-c_2),\ldots,f(x-c_k))+1.\]
|
||||||
The base case for the function is
|
The base case for the function is
|
||||||
|
@ -392,6 +392,7 @@ possibilities of dynamic programming.
|
||||||
|
|
||||||
\index{longest increasing subsequence}
|
\index{longest increasing subsequence}
|
||||||
|
|
||||||
|
Let us consider the following problem:
|
||||||
Given an array that contains $n$
|
Given an array that contains $n$
|
||||||
numbers,
|
numbers,
|
||||||
our task is to find the
|
our task is to find the
|
||||||
|
@ -399,7 +400,7 @@ our task is to find the
|
||||||
of the array.
|
of the array.
|
||||||
This is a sequence of array elements
|
This is a sequence of array elements
|
||||||
that goes from left to right,
|
that goes from left to right,
|
||||||
and each element in the sequence is larger
|
and each element of the sequence is larger
|
||||||
than the previous element.
|
than the previous element.
|
||||||
For example, in the array
|
For example, in the array
|
||||||
|
|
||||||
|
@ -487,23 +488,24 @@ that ends at position $k$ is constructed:
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item The subsequence
|
\item The subsequence
|
||||||
only contains the element at position $k$. In this case $f(k)=1$.
|
only contains the element at position $k$. In this case $f(k)=1$.
|
||||||
\item The subsequence is constructed
|
\item The subsequence contains a subsequence
|
||||||
by adding the element at position $k$ to
|
that ends at position $i$ where $i<k$,
|
||||||
a subsequence that ends at position $i$
|
followed by the element at position $k$.
|
||||||
where $i<k$ and the element at position $i$
|
The element at position $i$ must be smaller
|
||||||
is smaller than the element at position $k$. In this case $f(k)=f(i)+1$.
|
than the element at position $k$.
|
||||||
|
In this case $f(k)=f(i)+1$.
|
||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
|
|
||||||
For example, in the above example $f(7)=4$,
|
For example, in the above example $f(6)=4$,
|
||||||
because the subsequence $[2,5,7]$ of length 3
|
because the subsequence $[2,5,7]$ of length 3
|
||||||
ends at position 5, and by adding the element
|
ends at position 4, and by adding the element
|
||||||
at position 7 to this subsequence,
|
at position 6 to this subsequence,
|
||||||
we get the optimal subsequence $[2,5,7,8]$ of length 4.
|
we get the optimal subsequence $[2,5,7,8]$ of length 4.
|
||||||
|
|
||||||
An easy way to calculate the
|
An easy way to calculate the
|
||||||
value of $f(k)$ is to
|
value of $f(k)$ is to
|
||||||
go through all previous values
|
go through all previous values
|
||||||
$f(1),f(2),\ldots,f(k-1)$ and select the best solution.
|
$f(0),f(1),\ldots,f(k-1)$ and select the best solution.
|
||||||
The time complexity of such an algorithm is $O(n^2)$.
|
The time complexity of such an algorithm is $O(n^2)$.
|
||||||
Surprisingly, it is also possible to solve the
|
Surprisingly, it is also possible to solve the
|
||||||
problem in $O(n \log n)$ time. Can you see how?
|
problem in $O(n \log n)$ time. Can you see how?
|
||||||
|
@ -517,7 +519,7 @@ the lower-right corner such that
|
||||||
we only move down and right.
|
we only move down and right.
|
||||||
Each square contains a number,
|
Each square contains a number,
|
||||||
and the path should be constructed so
|
and the path should be constructed so
|
||||||
that the sum of numbers along
|
that the sum of the numbers along
|
||||||
the path is as large as possible.
|
the path is as large as possible.
|
||||||
|
|
||||||
The following picture shows an optimal
|
The following picture shows an optimal
|
||||||
|
@ -563,7 +565,7 @@ path in a grid:
|
||||||
\end{scope}
|
\end{scope}
|
||||||
\end{tikzpicture}
|
\end{tikzpicture}
|
||||||
\end{center}
|
\end{center}
|
||||||
The sum of numbers on the path is 67,
|
The sum of the numbers on the path is 67,
|
||||||
and this is the largest possible sum on a path
|
and this is the largest possible sum on a path
|
||||||
from the
|
from the
|
||||||
upper-left corner to the lower-right corner.
|
upper-left corner to the lower-right corner.
|
||||||
|
|
Loading…
Reference in New Issue