Skip to content

Longest Increasing Subsequence (LIS)

Rafiul Islam edited this page Nov 23, 2018 · 6 revisions

Example: arr[] = {10, 2, 1, 3, 5, 4, 6, 9, 8}

    for(int i=1; i<len; i++)
    {
        // check current's previous
        for(int j=0; j<i; j++)
        {
            // if current is smaller or equal to any of previous
            if(arr[j] >= arr[i])
                continue;
            // is current item's LIS is not less then previous value
            if(ans[j]+1 <= ans[i])
                continue;
            // set the current item's LIS value
            ans[i] = ans[j]+1;
        }
    }

Clone this wiki locally