最長遞增子序列問題

本文部分內容引用自:
https://blog.csdn.net/love20165104027/article/details/79618367
https://segmentfault.com/a/1190000012754802
如有侵權,請聯繫筆者刪除,郵箱: [email protected]

求最長遞增子序列長度的方法有兩種,第一種方法的時間複雜度爲 O(n^2),第二種方法的時間複雜度爲 O(nlogn)。

第一種方法代碼實現如下:

public static void getTheLengthOfLIS(){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for(int i = 0; i < n; i ++){
            array[i] = scanner.nextInt();
        }
        int[] dp = new int[n];
        dp[0] = 0;
        for(int i = 1; i < n; i ++){
            for(int j = i; j >= 0; j--){
                if(array[j] < array[i]){
                    dp[i] = dp[j]+1;
                    break;
                }
            }

        }
        int max = dp[0];
        for(int i = 1; i < n; i ++){
            if(max < dp[i]){
                max = dp[i];
            }
        }
        System.out.println(max+1);
}

第二種方法思想部分引用自:https://blog.csdn.net/love20165104027/article/details/79618367

思想如下:
假設存在一個序列d[1…9] ={ 2,1 ,5 ,3 ,6,4, 8 ,9, 7},可以看出來它的LIS長度爲5。
下面一步一步試着找出它。
我們定義一個序列B,然後令 i = 1 to 9 逐個考察這個序列。
此外,我們用一個變量Len來記錄現在最長算到多少了

首先,把d[1]有序地放到B裏,令B[1] = 2,就是說當只有1一個數字2的時候,長度爲1的LIS的最小末尾是2。這時Len=1

然後,把d[2]有序地放到B裏,令B[1] = 1,就是說長度爲1的LIS的最小末尾是1,d[1]=2已經沒用了,很容易理解吧。這時Len=1

接着,d[3] = 5,d[3]>B[1],所以令B[1+1]=B[2]=d[3]=5,就是說長度爲2的LIS的最小末尾是5,很容易理解吧。這時候B[1…2] = 1, 5,Len=2

再來,d[4] = 3,它正好加在1,5之間,放在1的位置顯然不合適,因爲1小於3,長度爲1的LIS最小末尾應該是1,這樣很容易推知,長度爲2的LIS最小末尾是3,於是可以把5淘汰掉,這時候B[1…2] = 1, 3,Len = 2

繼續,d[5] = 6,它在3後面,因爲B[2] = 3, 而6在3後面,於是很容易可以推知B[3] = 6, 這時B[1…3] = 1, 3, 6,還是很容易理解吧? Len = 3 了噢。

第6個, d[6] = 4,你看它在3和6之間,於是我們就可以把6替換掉,得到B[3] = 4。B[1…3] = 1, 3, 4, Len繼續等於3

第7個, d[7] = 8,它很大,比4大,嗯。於是B[4] = 8。Len變成4了

第8個, d[8] = 9,得到B[5] = 9,嗯。Len繼續增大,到5了。

最後一個, d[9] = 7,它在B[3] = 4和B[4] = 8之間,所以我們知道,最新的B[4] =7,B[1…5] = 1, 3, 4, 7, 9,Len = 5。

於是我們知道了LIS的長度爲5。

注意,這個1,3,4,7,9不是LIS,它只是存儲的對應長度LIS的最小末尾。有了這個末尾,我們就可以一個一個地插入數據。雖然最後一個d[9] = 7更新進去對於這組數據沒有什麼意義,但是如果後面再出現兩個數字 8 和 9,那麼就可以把8更新到d[5], 9更新到d[6],得出LIS的長度爲6。

然後應該發現一件事情了:在B中插入數據是有序的,而且是進行替換而不需要挪動——也就是說,我們可以使用二分查找,將每一個數字的插入時間優化到O(logN)~~~~~於是算法的時間複雜度就降低到了O(NlogN)~!

代碼實現如下:

    public static void getTheLengthOfLIS(){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for(int i = 0; i < n; i++){
            array[i] = scanner.nextInt();
        }
        int[] indexArray = new int[n];
        indexArray[0] = array[0];
        int len = 1;
        for(int i = 1; i < n; i++){
            int index = binarySearch(indexArray, 0, len-1, array[i]);
            if(index >= len){
                len++;
            }
            indexArray[index] = array[i];
        }
        System.out.println(len);
    }

    public static int binarySearch(int[] indexArray, int start, int end, int value){
        if(start == end){
            if(value <= indexArray[start]){
                return start;
            }else{
                return start+1;
            }
        }
        int mid = start + (end - start)/2;
        if(value == indexArray[mid]){
            return mid;
        }
        if(value < indexArray[mid]){
            if(value > indexArray[mid-1]){
                return mid;
            }
            return binarySearch(indexArray, start, mid, value);
        }
        return binarySearch(indexArray, mid+1, end, value);
    }

上述第二種方法如果最終要獲取最長遞增子序列的具體序列的話,思想引用自:
https://segmentfault.com/a/1190000012754802

假設已知序列爲{5,1,6,8,2,4,5,10}
---------------------------------------------------------------------------
step1: 5
---------------------------------------------------------------------------
step2: 1
	   5 (discard)
---------------------------------------------------------------------------
step3: 1,6
---------------------------------------------------------------------------
step4: 1,6,8
---------------------------------------------------------------------------
step5: 1,6,8
	   1,2
---------------------------------------------------------------------------
step6: 1,2,4
	   1,6,8(discard)
---------------------------------------------------------------------------
step7: 1,2,4,5
---------------------------------------------------------------------------
step8: 1,2,4,5,10
---------------------------------------------------------------------------
end

每次的元素如果比已存在的序列的最後一個元素小,這個小元素更有可能構成長度較長的序列。如1,6,4,5 當序列爲1,6時將6換成4更有可能構成長度更長的隊列。
之所以要存儲後來的更小的元素爲一個新序列,是因爲可能由這個更小的元素構成的遞增子序列更長。

代碼如下:

public static void getTheLengthOfLIS(){
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for(int i = 0; i < array.length; i++){
            array[i] = scanner.nextInt();
        }
        
        //sequence 用於存儲每個可能的序列
        ArrayList<ArrayList<Integer>> sequenceList = new ArrayList<>();
        
        ArrayList<Integer> firstList = new ArrayList<>();
        firstList.add(array[0]);
        sequenceList.add(firstList);
        for(int i = 1; i < array.length; i++){
            for(int j = 0; j < sequenceList.size(); j++){
                ArrayList<Integer> list = sequenceList.get(j);
                int listSize = list.size();
                if(array[i] > list.get(listSize-1)){
                    list.add(array[i]);
                }else if(array[i] < list.get(listSize-1)){
                    if(listSize >= 2){
                        if( array[i] > list.get(listSize-2)){
                            list.set(listSize-1, array[i]);
                        }else if(array[i] < list.get(listSize-2)) {
                            ArrayList<Integer> addList = new ArrayList<>();
                            for (int k = 0; k < listSize; k++) {
                                if (list.get(k) < array[i]) {
                                    addList.add(list.get(k));
                                } else {
                                    addList.add(array[i]);
                                    break;
                                }
                            }
                            sequenceList.add(addList);
                        }
                    }else{
                        list.set(listSize-1, array[i]);
                    }
                }

            }
        }
        int maxLength = 0;
        for(int i = 0; i < sequenceList.size(); i++){
            if(maxLength < sequenceList.get(i).size()){
                maxLength = sequenceList.get(i).size();
            }
        }
        System.out.println(maxLength);
        //sequenceList 中最長的序列即爲最長遞增子序列。
    }

感覺時間複雜度也不小,如有更好的方法,歡迎指正,謝謝。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章