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?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

思路:这道题我最开始以为和最大长方形那道题一样呢,用一个栈就能解决。结果试了一下,样例不通过。
看看O(N^2)的复杂度,果断动态规划。简单的动态规划不难,问题是O(NlogN)的那个真没想出来,挺巧妙的。
class Solution {
    /**
    * 普通的动态规划,对于每一个位置,计算到此为止位置的最大长度,每个dp中保存的是到每个位置为止的最大长度,复杂度N^2
    */
    public int lengthOfLIS1(int[] nums) {
        if(nums.length == 0) return 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[i] > nums[j]){
                    dp[i] = Math.max(dp[i],dp[j] + 1) ;
                }
            }
        }
        int max = 0;
        for(int i = 0; i<nums.length ; i++){
            max = Math.max(max,dp[i]);
        }
        return max;
    }
    
    /**
    *改进的动态规划,整个dp数组中保存的是最大长度的子序列(len表示其长度),而且是高度最矮的那条子序列(因为在0到len范围二分搜索时,如果新插入的值比原值     *小,则将原值替换掉了),并且因为最大值始终插入到dp数组末尾,所以dp数组可以使用二分搜索
    */
    public int lengthOfLIS(int[] nums) {
        if(nums.length == 0) return 0;
        int[] dp = new int[nums.length];
        int len = 0;
        for(int num :nums){
            int i = Arrays.binarySearch(dp,0,len,num);
            if(i<0) i = - (i + 1);//这个和自动实现的二分搜索算法返回值有关,求出的是应该插入的位置(当前数组位置插)
            dp[i] = num;
            if(i == len ){
             len++;   
            }
        }
        return len;
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值