Redian新闻
>
longest increasing subsequence O(NlogN)算法中数组 P 是否必
avatar
longest increasing subsequence O(NlogN)算法中数组 P 是否必# JobHunting - 待字闺中
B*M
1
http://en.wikipedia.org/wiki/Longest_increasing_subsequence
Efficient algorithms
The algorithm outlined below solves the longest increasing subsequence
problem efficiently, using only arrays and binary searching. It processes
the sequence elements in order, maintaining the longest increasing
subsequence found so far. Denote the sequence values as X[1], X[2], etc.
Then, after processing X[i], the algorithm will have stored values in two
arrays:
M[j] — stores the position k of the smallest value X[k] such that there
is an increasing subsequence of length j ending at X[k] on the range k ≤ i
(note we have j ≤ k ≤ i here).
P[k] — stores the position of the predecessor of X[k] in the longest
increasing subsequence ending at X[k].
In addition the algorithm stores a variable L representing the length of the
longest increasing subsequence found so far.
Note that, at any point in the algorithm, the sequence
X[M[1]], X[M[2]], ..., X[M[L]]
is nondecreasing. For, if there is an increasing subsequence of length i
ending at X[M[i]], then there is also a subsequence of length i-1 ending at
a smaller value: namely the one ending at X[P[M[i]]]. Thus, we may do binary
searches in this sequence in logarithmic time.
这里的数组P是否必要??
avatar
S*n
2
如果你想返回那个数组的话就需要,如果只需要那个数组长度就不需要

http://en.wikipedia.org/wiki/Longest_increasing_subseq
uence
increasing subsequence
searching. It processes
longest increasing
as X[1], X[2], etc.
stored values in two
value X[k] such that there
X[k] on the range k ≤ i

【在 B*M 的大作中提到】
: http://en.wikipedia.org/wiki/Longest_increasing_subsequence
: Efficient algorithms
: The algorithm outlined below solves the longest increasing subsequence
: problem efficiently, using only arrays and binary searching. It processes
: the sequence elements in order, maintaining the longest increasing
: subsequence found so far. Denote the sequence values as X[1], X[2], etc.
: Then, after processing X[i], the algorithm will have stored values in two
: arrays:
: M[j] — stores the position k of the smallest value X[k] such that there
: is an increasing subsequence of length j ending at X[k] on the range k ≤ i

avatar
B*M
3
我run了程序,去掉P,一样可以返回数组,
不知道是不是我的test case太简单了,
我试了好几个,都没试出来,P什么时候才是必要的,
分析也看不出来,好像M就足够了,

【在 S******n 的大作中提到】
: 如果你想返回那个数组的话就需要,如果只需要那个数组长度就不需要
:
: http://en.wikipedia.org/wiki/Longest_increasing_subseq
: uence
: increasing subsequence
: searching. It processes
: longest increasing
: as X[1], X[2], etc.
: stored values in two
: value X[k] such that there

avatar
j*u
4
没有仔细看wiki,如果要返回数组是需要2个array的
比如这个例子: 5 6 7 1 2
最终M = {3, 4, 2},如果没有P,返回的数组就成了1 2 7而不是5 6 7了。

【在 B*M 的大作中提到】
: 我run了程序,去掉P,一样可以返回数组,
: 不知道是不是我的test case太简单了,
: 我试了好几个,都没试出来,P什么时候才是必要的,
: 分析也看不出来,好像M就足够了,

avatar
a*y
5
try 10 20 30 40 50 1 2
what in M would be 1 2 30 40 50

【在 B*M 的大作中提到】
: 我run了程序,去掉P,一样可以返回数组,
: 不知道是不是我的test case太简单了,
: 我试了好几个,都没试出来,P什么时候才是必要的,
: 分析也看不出来,好像M就足够了,

avatar
B*M
6
您老英明,果然如此!!
多谢!!

【在 j*****u 的大作中提到】
: 没有仔细看wiki,如果要返回数组是需要2个array的
: 比如这个例子: 5 6 7 1 2
: 最终M = {3, 4, 2},如果没有P,返回的数组就成了1 2 7而不是5 6 7了。

avatar
B*M
7
多谢!

【在 a***y 的大作中提到】
: try 10 20 30 40 50 1 2
: what in M would be 1 2 30 40 50

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。