国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LintCode] Longest Increasing Subsequence

Flands / 2589人閱讀

Problem

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

https://en.wikipedia.org/wiki...

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)

Solution DP O(n^2)
public class Solution {
    public int longestIncreasingSubsequence(int[] nums) {
        int n = nums.length, max = 0;
        int[] dp = new int[n];
        for (int i = 0; i < n; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                dp[i] = nums[i] >= nums[j] ? Math.max(dp[j]+1, dp[i]) : dp[i];
                max = Math.max(max, dp[i]);
            }
        }
        return max;
    }
}
Binary Search O(nlogn)
public class Solution {
    public int longestIncreasingSubsequence(int[] nums) {
        int n = nums.length, max = 0;
        if (n == 0) return 0;
        int[] tails = new int[nums.length];
        tails[0] = nums[0];
        int index = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] < tails[0]) tails[0] = nums[i];
            else if (nums[i] >= tails[index]) tails[++index] = nums[i];
            else tails[bisearch(tails, 0, index, nums[i])] = nums[i];
        }
        return index+1;
    }
    public int bisearch(int[] tails, int start, int end, int target) {
        while (start <= end) {
            int mid = start + (end-start)/2;
            if (tails[mid] == target) return mid;
            else if (tails[mid] < target) start = mid+1;
            else end = mid-1;
        }
        return start;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/64973.html

相關(guān)文章

  • [Lintcode] Longest Increasing Subsequence 最長上升序列

    摘要:樣例給出,這個是,返回給出,這個是,返回挑戰(zhàn)要求時間復(fù)雜度為或者說明最長上升子序列的定義最長上升子序列問題是在一個無序的給定序列中找到一個盡可能長的由低到高排列的子序列,這種子序列不一定是連續(xù)的或者唯一的。 Longest Increasing Subsequence 本文最新版本位于 https://yanjia.me/zh/2018/11/... 給定一個整數(shù)序列,找到最長上升子序...

    hlcc 評論0 收藏0
  • [LintCode] Longest Increasing Continuous Subseque

    摘要:最長連續(xù)遞增遞減子序列,設(shè)置正向計數(shù)器,后一位增加則計數(shù)器加,否則置。反向計數(shù)器亦然。每一次比較后將較大值存入。 Problem 最長連續(xù)遞增/遞減子序列 Give an integer array,find the longest increasing continuous subsequence in this array.An increasing continuous subs...

    wwq0327 評論0 收藏0
  • Longest Increasing Subsequence

    摘要:解題思路求不必連續(xù)的最長升序字符串長度,采用動態(tài)規(guī)劃,利用狀態(tài)表示以字符結(jié)尾的最長子串的長度,那么狀態(tài)轉(zhuǎn)移方程就是且必須小于另外還需維護(hù)一個最大長度。 Longest Increasing SubsequenceGiven an unsorted array of integers, find the length of longest increasing subsequence. ...

    yangrd 評論0 收藏0
  • Longest Increasing Subsequence

    摘要:題目鏈接主要兩種方法和用,就是每次找出為結(jié)尾的最長的串的長度就好了。所以分解成就是,這個復(fù)雜度是。用一個數(shù)組,表示的長度為,表示長度為的,最右邊的可能的最小值。這里只要求長度即可,那就直接用就可以了,整個用個數(shù)組就行了。 Longest Increasing Subsequence 題目鏈接:https://leetcode.com/problems... 主要兩種方法:dp和gree...

    FullStackDeveloper 評論0 收藏0
  • [LeetCode] 300. Longest Increasing Subsequence

    Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest increasing subsequence is [2,3,7...

    luckyyulin 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<