码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode Longest Increasing Subsequence

时间:2016-08-13 06:35:11      阅读:361      评论:0      收藏:0      [点我收藏+]

标签:

Given a sequence of integers, find the longest increasing subsequence (LIS).

You code should return the length of the LIS.

Clarification

What‘s the definition of longest increasing subsequence?

  • The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence‘s elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.

  • https://en.wikipedia.org/wiki/Longest_increasing_subsequence

Example:
For [5, 4, 1, 2, 3], the LIS is [1, 2, 3], return 3
For [4, 2, 4, 5, 3, 7], the LIS is [2, 4, 5, 7], return 4
Challenge:

Time complexity O(n^2) or O(nlogn)

For this problem we should think about dynamic programming. For dynamic programming, the key point is to think about the problem relationship between prblem of DP(N) and the sub problems. So in order to think about wheather we need to add a number into the longest sequence, we could choose either choose or not. This will lead to O(2^n) problem because the sitution we need to compare is a combination problem which we will either choose on element in the array or not. Using another array to store the result of the problem with only subarry which include all the elements before.

1. define the problem result(n) returns the number of longest increasing sequence of problem with array with length n

2. using two pointers i and j to indicate the current largest at point i, and j will traverse from first to the latest element which stand for the array with length j‘s longest increasing subsequence before reach i. result(i) =  max(result(i), result(j)+1)

3. base case is when array length is 1, the result should be 1 because you have one elemnt which is increasing order

4. solve result(n)

Using a for loop to traverse all the element in the result array which stand for to solve the problem of longest increasing subsequence of length i, then we check the result of longest increasing subsequence of array with length j which j < i and compare the result[j] + 1 with current result[i]. We keep to update the larger one to the result[i].

First solution:

 

public class Solution {
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    public int longestIncreasingSubsequence(int[] nums) {
        // write your code here
        int[] result = new int [nums.length];
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            result[i] = 1;
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    result[i] = Math.max(result[i], result[j] + 1);
                }
            }
            max = Math.max(result[i],max);
        }
        return max;
    }
}

Second Solution:

Using an greedy algorithm to include all the increasing sequence which is a greedy algorithm,

 

public class Solution {
    /**
     * @param nums: The integer array
     * @return: The length of LIS (longest increasing subsequence)
     */
    public int longestIncreasingSubsequence(int[] nums) {
        // write your code here
        if (nums == null || nums.length == 0){
            return 0;
        }
        ArrayList<Integer> res = new ArrayList<Integer>();
       for (int num : nums){// use a for loop to traverse all the number in array nums
                 // if the current number in the original array is larger than the last number in result array
                 // that stores the increasing sequence and if there is any number larger or equals to some existing number
                // in the result array we just replace it with the old ones
if (res.size() == 0 || res.get(res.size() - 1) < num){ res.add(num); } else{ int start = 0; int end = res.size() - 1; while (start < end){ int mid = start + (end - start) / 2; if (res.get(mid) < num){ start = mid + 1; } else if (res.get(mid) > num){ end = mid; } else{ start = end = mid; } } res.set(start, num); } } return res.size(); } }

 

LintCode Longest Increasing Subsequence

标签:

原文地址:http://www.cnblogs.com/ly91417/p/5767112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!