LintCode 最長上升子序列

給定一個整數序列,找到最長上升子序列(LIS),返回LIS的長度。

說明

最長上升子序列的定義:

最長上升子序列問題是在一個無序的給定序列中找到一個盡可能長的由低到高排列的子序列,這種子序列不一定是連續的或者唯一的。
https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

樣例

給出[5,4,1,2,3],這個LIS是[1,2,3],返回 3

給出[4,2,4,5,3,7],這個LIS是[4,4,5,7],返回 4

分析

第一種做法(常規做法):

時間復雜度:O(n^2)

public class Solution {
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    public int longestIncreasingSubsequence(int[] nums) {
        if(null == nums || nums.length <= 0)
            return 0;
        int[] logestSequences = new int[nums.length + 1];
        logestSequences[0] = 1;
        for(int i = 1;i < nums.length;i++)
        {
            logestSequences[i] = 1;
            for(int j = 0;j < i;j++)
            {
                if(nums[i] >= nums[j] && (logestSequences[j] + 1 > logestSequences[i])) // 核心代碼
                {
                    logestSequences[i] = logestSequences[j] + 1;
                }
            }
        }
        
        int longestIncreasingSubsequence = -1;
        for(int i = 0;i < nums.length;i++)
        {
            if(logestSequences[i] > longestIncreasingSubsequence)
            {
                longestIncreasingSubsequence = logestSequences[i];
            }
        }
        return longestIncreasingSubsequence;
    }
}

第二種做法(貪心策略):

時間復雜度:O(n*lg(n))

分析:開一個棧,每次取棧頂元素top和讀到的元素temp做比較,如果temp > top 則將temp入棧;如果temp < top則二分查找棧中的比temp大的第1個數,并用temp替換它。 最長序列長度即為棧的大小top。
這也是很好理解的,對于x和y,如果x < y且Stack[y] < Stack[x],用Stack[x]替換Stack[y],此時的最長序列長度沒有改變但序列Q的''潛力''增大了。

舉例:原序列為1,5,8,3,6,7
棧為1,5,8,此時讀到3,用3替換5,得到1,3,8; 再讀6,用6替換8,得到1,3,6;再讀7,得到最終棧為1,3,6,7。最長遞增子序列為長度4。

public class Solution {
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    public int longestIncreasingSubsequence(int[] nums) {
        if(null == nums || nums.length <= 0)
            return 0;
        if(nums.length == 1)
            return 1;
        int[] subsequenceStack = new int[nums.length + 1];
        int top = 0;
        subsequenceStack[0] = -1;
        
        for(int i = 0;i < nums.length;i++)
        {
            if(nums[i] >= subsequenceStack[top])
            {
                top++;
                subsequenceStack[top] = nums[i];
            }
            else
            {
                int low = 0;
                int high = top;
                while(low <= high)
                {
                    int middle = (low + high) / 2;
                    if(nums[i] < subsequenceStack[middle])
                    {
                        high = middle - 1;
                    }
                    else
                    {
                        low = middle + 1;
                    }
                }
                subsequenceStack[low] = nums[i];
            }
        }
        return top;
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容