Maximum incremental subsequence with dynamic programming

The problem is this: If a sequence L of n integers is not necessarily different, write an algorithm that calculates an increasing subsequence of maximum length:

The recurrence equation I developed is this:

I start the index with 0:

If j = n opt(j) = 0 (base case)
otherwise opt(j) = max j <i <= n such that Lj <Li = {opt(i) +1}

Do you think this is right? the standard solution used for this typical task is to first calculate the maximum increasing subsequence ending in Li for all elements of the sequence, and then the maximum at these values, i.e.:

if i = 1 opt (i) = 1
otherwise opt (i) = max 1 <= j <= i-1 and Lj <Li = {opt (i)} +1

and then maximum on these elements.

so I wanted to know if you think my decision was right.

+3
source
2

: , , , , k = . , [0... n], k.

+1

// , .

int longsub (int a[], int len) {

    int localsum = 0;
    int i = 0;
    int begin = i;
    int localsublen = 1;
    int globalsunlen = 0;
    int end = i;

    for (i=1; i< len; i++)    {

        if (a[i] > a[i-1]) {
              localsublen++;
        }
        else {
            newbegin = i;
            localsublen = 1;
        }

        if (localsublen > globalsublen)    {
            begin = newbegin;
            end = i;
            globalsublen = localsublen;
        }
    }

    for (i=begin;i <= end; i++)    
        printf ("%d.\n",a[i]);


}
0

Source: https://habr.com/ru/post/1789805/


All Articles