简体   繁体   English

如何检查每个元素是否在数组的任何最长递增子序列中?

[英]How to check whether each element is in any Longest Increasing Subsequence of an array or not?

We have an array of integer numbers. 我们有一个整数数组。 We want to know for each element whether that element is contained at least in one LIS of many LISs of our array or not. 我们想知道每个元素是否至少包含在我们数组的许多LIS中的一个LIS中。 We want to know this for all elements in the array in less than O(n 2 ) . 我们想知道小于O(n 2 )的所有元素。

For example array [2, 4, 3, 2, 5] has two LISs. 例如,数组[2、4、3、2、5]具有两个LIS。 All elements in the array belong to at least one of these LISs, exept the 4 th element which does not belong to any LIS. 数组中的所有元素至少属于这些LIS之一,第4 元素不属于任何LIS。

I know an easy solution which uses dfs , but its runtime is O(n 2 ) . 我知道一个使用dfs的简单解决方案,但其运行时为O(n 2

Run an algorithm such as https://en.wikipedia.org/wiki/Longest_increasing_subsequence#Efficient_algorithms which computes, at each point, the length of the longest increasing subsequence ending at that point. 运行一种算法,例如https://en.wikipedia.org/wiki/Longest_increasing_subsequence#Efficient_algorithms ,该算法在每个点上计算到此点为止最长的递增子序列的长度。

Run the same algorithm using the data in reversed order, to compute, for each point, the length of the longest increasing subsequence starting at that point. 使用相反的数据运行相同的算法,以针对每个点计算从该点开始的最长递增子序列的长度。

For each point add the two computed lengths. 对于每个点,添加两个计算出的长度。 The point is on a longest increasing subsequence if this sum is equal to the largest sum found. 如果此总和等于找到的最大总和,则该点位于最长的递增子序列上。

The alogorithm quoted takes time O(n log n) for each pass and the sum is only O(log n) so the total is O(n log n) 引用的算法每次通过需要时间O(n log n),总和仅为O(log n),所以总数为O(n log n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM