LIS better
This commit is contained in:
parent
39e2981355
commit
d8a26a6274
30
luku07.tex
30
luku07.tex
|
@ -465,7 +465,7 @@ Let $f(k)$ be the length of the
|
||||||
longest increasing subsequence
|
longest increasing subsequence
|
||||||
that ends at position $k$.
|
that ends at position $k$.
|
||||||
Using this function, the answer to the problem
|
Using this function, the answer to the problem
|
||||||
is the largest of values
|
is the largest of the values
|
||||||
$f(1),f(2),\ldots,f(n)$.
|
$f(1),f(2),\ldots,f(n)$.
|
||||||
For example, in the above array
|
For example, in the above array
|
||||||
the values of the function are as follows:
|
the values of the function are as follows:
|
||||||
|
@ -487,27 +487,23 @@ there are two possibilities how the subsequence
|
||||||
that ends at position $k$ is constructed:
|
that ends at position $k$ is constructed:
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item The subsequence
|
\item The subsequence
|
||||||
only contains the element $x_k$, so $f(k)=1$.
|
only contains the element $x_k$. In this case $f(k)=1$.
|
||||||
\item We choose some position $i$ for which $i<k$
|
\item The subsequence is constructed
|
||||||
and $x_i<x_k$.
|
by adding the element $x_k$ to
|
||||||
We extend the longest increasing subsequence
|
a subsequence that ends at position $i$
|
||||||
that ends at position $i$ by adding the element $x_k$
|
where $i<k$ and $x_i<x_k$. In this case $f(k)=f(i)+1$.
|
||||||
to it. In this case $f(k)=f(i)+1$.
|
|
||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
|
|
||||||
Consider calculating the value of $f(7)$.
|
For example, in the above example $f(7)=4$,
|
||||||
The best solution is to extend the longest
|
because the subsequence $[2,5,7]$ of length 3
|
||||||
increasing subsequence that ends at position 5,
|
ends at position 5, and after adding the element
|
||||||
i.e., the sequence $[2,5,7]$, by adding
|
at position 7 to the subsequence,
|
||||||
the element $x_7=8$.
|
we get the optimal subsequence $[2,5,7,8]$ of length 4.
|
||||||
The result is
|
|
||||||
$[2,5,7,8]$, and $f(7)=f(5)+1=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
|
||||||
inspect all positions
|
go through all previous values
|
||||||
$i=1,2,\ldots,k-1$ that can contain the
|
$f(1),f(2),\ldots,f(k-1)$ and select the best solution.
|
||||||
previous element in the subsequence.
|
|
||||||
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, but this is more difficult.
|
problem in $O(n \log n)$ time, but this is more difficult.
|
||||||
|
|
Loading…
Reference in New Issue