300. Longest Increasing Subsequence

11 篇文章 0 订阅

Given an integer array nums, return the length of the longest strictly
increasing subsequence.

A subsequence is a sequence that can be derived from an array by
deleting some or no elements without changing the order of the
remaining elements. For example, [3,6,2,7] is a subsequence of the
array [0,3,1,6,2,2,7].

Example 1:

Input: nums = [10,9,2,5,3,7,101,18]
Output: 4
Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
Example 2:

Input: nums = [0,1,0,3,2,3]
Output: 4
Example 3:

Input: nums = [7,7,7,7,7,7,7]
Output: 1

Constraints:

1 <= nums.length <= 2500
-104 <= nums[i] <= 104

求数组中递增子数组的最大长度, 可以用DP来储存当前位置的递增最大子数组。 比如[10,9,2,5,3,7,101,18],首先在10位置的最大子数组长度为1, 9位置也为1, 2位置也是1, 5位置是2,因为【2,5】是一个长度为2的递增子数组(父数组是【10,9,2,5】)。

public int lengthOfLIS(int[] nums) {
	if (nums.length == 0) return 0;
	int[] dp = new int[nums.length];
	dp[0] = 1;
	int maxLen = 1;
	// 遍历数组, 找到截止每个数的最长递增子数列长度
	for (int i = 1; i < dp.length; i++) {
		int currMax = 0;
		for (int j = 0; j < i; j++) {
			// 做一个剪枝, 只有在前面的数比当前数小的时候,才求dp长度
			if (nums[j] < nums[i]) {
				// 会有好几个比nums[i] 小的数,因此求出其中的最大值,实际上是一个递归的公式
				currMax = Math.max(currMax, dp[j]);
			}
			dp[i] = currMax + 1;
			maxLen = Math.max(dp[i], maxLen);
		}		
	}
	return maxLen;
}

时间复杂度O(N^2)

  1. Smarter way (BinarySearch)
    参考

https://leetcode.com/problems/longest-increasing-subsequence/discuss/74824/JavaPython-Binary-search-O(nlogn)-time-with-explanation

tails储存每个可能的长度的数组,tails[0]存储长度为1的所有数组的尾部的最小值,依次递增……比如说示例数组 [10,9,2,5,3,7,101,18], tails[0] 是所有长度为1 的数组, 尾部取最小,tais[0] = 2; tails[1] 是所有长度为2的数组的尾部,有[2,5], [5,7], [2,7], [3, 7], [10, 101], [10, 18], [9, 101], [9, 18], [2,101]…tails[1] = 5

这样取最小尾部的目的是为了比较当前遍历的数与其大小,如果当前遍历的数比某个长度的尾部大的时候,直接长度+1就可以; 又如果当前遍历的数在两个相邻tails之间,那么就更新当前的tails使得tails永远储存的都是最小尾部(比如tails【0】 = 9的时候,我们遍历到了2, 那么就要更新tails【0】 = 2)

public int lengthOfLIS(int[] nums) {
    int[] tails = new int[nums.length];
    int size = 0;
    for (int x : nums) {
        int i = 0, j = size;
        while (i != j) {
            int mid = (i + j) / 2;
            if (tails[mid] < x)
                i = mid + 1;
            else
                j = mid;
        }
        tails[i] = x;
        if (i == size) {
        	size++;
        }
    }
    return size;
}

下面的ppt可以更好地理解二分法

Princeton的patience sorting PPT

princeton是真的厉害,二分法的晦涩难懂瞬间恍然大悟。
算法:
大家都玩过蜘蛛纸牌, 现在把这一个数组看成一叠纸牌,我们需要找到若干个堆, 每个堆要保持降序比如数组 [10,9,2,5,3,7,101,18], 首先第一个堆是10,9,2, 第二个堆是5,3,依次下去。 在保持最小堆得总量的情况下,递增数列的最大长度就是堆得数量。
(Prof: 保持最小数量的堆,就是每次在堆得降序排列中,遍历到比当前头部数小的数中最小的数,比如10是头部的时候,要遍历到2, 如果只遍历到9, 那么2就会单独成一个堆,那么堆得size就不是最小的,不方便找到最长递增子数列。因为我们要跳过单独2的堆来查看)

patience sorting的时间复杂度可以控制在O(NlogN)

class Solution {
    public int lengthOfLIS(int[] nums) {
        List<Integer> piles = new ArrayList<>(nums.length);
        for (int num : nums) {
            int index = Collections.binarySearch(piles, num);
            if (index < 0) index = ~index;
            if (index == piles.size()) {
                piles.add(num);
            } else {
                piles.set(index, num);
            }
        }
        return piles.size();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值