[LintCode]76. Longest Increasing Subsequence

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

You code should return the length of the LIS.

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)

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.
public class Solution {
    /**
     * @param nums: An integer array
     * @return: The length of LIS (longest increasing subsequence)
     */

    public int longestIncreasingSubsequence(int[] nums) {
        if(nums == null || nums.length <= 0) {
            return 0;
        }
        int len = nums.length;
        int[] cnt = new int[len];
        int res = 1;
        cnt[0] = 1;
        for(int i = 1; i < len; i++) {
            cnt[i] = 1;  // 最少的也是1个,初始化的时候容易初始化为0
            for(int j = 0; j < i; j++) {
                if(nums[j] < nums[i]) {
                    if(cnt[i] < cnt[j] + 1) {
                        cnt[i] = cnt[j]+1;
                        res = Math.max(res, cnt[i]);
                    }
                }
            }
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/lawrenceSeattle/p/10276209.html

时间: 2024-10-11 10:28:56

[LintCode]76. Longest Increasing Subsequence的相关文章

LintCode Longest Increasing Subsequence

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

LintCode刷题笔记--Longest Increasing Subsequence

动态规划 描述: 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 t

[Coding Made Simple] Longest Increasing Subsequence

Given an array, find the longest increasing subsequence in this array. The same problem link. [LintCode] Longest Increasing Subsequence

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

300. Longest Increasing Subsequence

Problem statement: 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

SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre

Dynamic Programming | Set 3 (Longest Increasing Subsequence)

在 Dynamic Programming | Set 1 (Overlapping Subproblems Property) 和 Dynamic Programming | Set 2 (Optimal Substructure Property) 中我们已经讨论了重叠子问题和最优子结构性质,现在我们来看一个可以使用动态规划来解决的问题:最长上升子序列(Longest Increasing Subsequence(LIS)). 最长上升子序列问题,致力于在一个给定的序列中找到一个最长的子序列

Longest Increasing Subsequence

Longest Increasing Subsequence(LIS) 一个美丽的名字 非常经典的线性结构dp [朴素]:O(n^2) d(i)=max{0,d(j) :j<i&&a[j]<a[i]}+1 直接两个for [二分查找优化]:O(n^2) g(i):d值为i的最小的a  每次更新然后lower_bound即可 [大于等于] lower_boundReturn iterator to lower bound Returns an iterator pointing

[LeetCode][JavaScript]Longest Increasing Subsequence

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. Not