Longest Increasing Subsequence

35 篇文章 0 订阅
11 篇文章 0 订阅

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

You code should return the length of the LIS.

Have you met this question in a real interview?  
Yes
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 [4, 4, 5, 7], return 4

Challenge

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

思路: O(N^2) Using dp to store the current length max LIS.

O(nLogn)  参考 使用dp[i] 来存储一个当前长度为i , 以dp[i] 为右端的最小的串。每次查找更新的时候用二分法。


O(n ^ 2)

public int longestIncreasingSubsequence(int[] nums) {
        // write your code here
        int len = nums.length;
        if(len < 1)
            return 0;
        int max = 1;
        int[] dp = new int[len];
        for(int i = 0 ; i < len; i++){
        	dp[i] = 1;
        }
        
        for(int i = len - 1; i >= 0; i--){
        	for(int j = i + 1; j < len; j++){
        		if(nums[j] >= nums[i]){
        			dp[i] = Math.max(dp[i], dp[j] + 1);
        			max = Math.max(max, dp[i]);
        		}
        	}
        }
        return max;
    }
O(N*LogN)

public int longestIncreasingSubsequence1(int[] nums){
		int len = nums.length;
		if(len < 1)
			return 0;
		int max = 1;
		int[] dp = new int[len + 1];
		dp[1] = nums[0];
		for (int i = 1; i < len; ++i) {
			if (dp[max] <= nums[i]) {
				dp[++max] = nums[i];
			} else {
				int pos = find(i, max, nums, dp);
				dp[pos] = nums[i];
			}
		}
		return max;
	}
	
	private int find(int idx, int max, int[] num, int[] dp)
	{
		int l = 1;
		int r = max - 1;
		//二分找到更新的位置
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (num[idx] > dp[mid] && num[idx] <= dp[mid + 1]) {
				return mid + 1;
			} else if (num[idx] > dp[mid]) {
				l = mid + 1;
			} else {
				r = mid - 1;
			}
		}
		//找不到,就更新d[1],这个一定是长度为1的最小的
		//否则,就到不了这一步了
		return 1;
	}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值