300 Longest Increasing Subsequence 最长递增子序列

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(n2) complexity.

Follow up: Could you improve it to O(n log n) time complexity?

解法1:动态规划

public int lengthOfLIS(int[] nums) {
        //[10, 2, 5, 3, 7],
    if(nums==null || nums.length<1) return 0;

    int [] d = new int[nums.length];
    d[0] = 1;
    int max = 1;
    for(int i=1; i<nums.length; i++) {
        d[i] = 1;
        for(int j=0; j<i; j++) {
            if(nums[i] > nums[j]) {
                d[i] = Math.max(d[i], d[j]+1);
            }
        }
        max = Math.max(max, d[i]);
    }
    return max;
}

Maintain a potential LIS arraylist

for each element n, append it to the tail of LIS if its the largest O/W binary search the insert position in the LIS, then replace the original element with n.

public int lengthOfLIS(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        ArrayList<Integer> lis = new ArrayList<Integer>();
        for (int n : nums) {
            if (lis.size() == 0 || lis.get(lis.size() - 1) < n) {
                lis.add(n);
            } else {
                updateLIS(lis, n);
            }
        }
        return lis.size();
}

private void updateLIS(ArrayList<Integer> lis, int n) {
        int l = 0, r = lis.size() - 1;
        while (l < r) {
            int m = l + (r - l) / 2;
            if (lis.get(m) < n) {
                l = m + 1;
            } else {
                r = m;
            }
        }
        lis.set(l, n);
}



public int lengthOfLIS(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int dp[] = new int[nums.length];
        int len = 0;

        for (int cur : nums) {
            int index = Arrays.binarySearch(dp, 0, len, cur);

            if(index < 0)
                index = -(index + 1);
            dp[index] = cur;

            if(index == len)
                len++;
        }
        return len;
}

results matching ""

    No results matching ""