longest nondecreasing subsequence in O(nlgn)

前端 未结 5 1936
无人及你
无人及你 2021-01-06 04:07

I have the following algorithm which works well

I tried explaining it here for myself http://nemo.la/?p=943 and it is explained here http://www.geeksforgeeks.org/lon

5条回答
  •  粉色の甜心
    2021-01-06 04:37

    To find the longest non-strictly increasing subsequence, change these conditions:

    1. If A[i] is smallest among all end candidates of active lists, we will start new active list of length 1.
    2. If A[i] is largest among all end candidates of active lists, we will clone the largest active list, and extend it by A[i].
    3. If A[i] is in between, we will find a list with largest end element that is smaller than A[i]. Clone and extend this list by A[i]. We will discard all other lists of same length as that of this modified list.

    to:

    1. If A[i] is smaller than the smallest of all end candidates of active lists, we will start new active list of length 1.
    2. If A[i] is largest among all end candidates of active lists, we will clone the largest active list, and extend it by A[i].
    3. If A[i] is in between, we will find a list with largest end element that is smaller than or equal to A[i]. Clone and extend this list by A[i]. We will discard all other lists of same length as that of this modified list.

    The fourth step for your example sequence should be:

    10 is not less than 10 (the smallest element). We find the largest element that is smaller than or equal to 10 (that would be s[0]==10). Clone and extend this list by 10. Discard the existing list of length 2. The new s becomes {10 10}.

提交回复
热议问题