Longest Increasing Subsequence(最长上升子序列)

http://www.lintcode.com/en/problem/longest-increasing-subsequence/?rand=true

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.length == 0) {
            return 0;
        }
        int max = 0;
        int[] dp = new int[nums.length];
//        存储最长的子数组长度
        for (int i = 0; i < nums.length; i++) {
            dp[i] = 1;
//            至少自己一个是可以的
            for (int j = 0; j < i; j++) {
                if (nums[j] < nums[i]) {
//                    说明可以在j位置上的最长子数组加1
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            max = Math.max(dp[i], max);
        }
        return max;
    }
}

你可能感兴趣的:(Longest Increasing Subsequence(最长上升子序列))