zoukankan      html  css  js  c++  java
  • LintCode-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 [4, 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.  

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

    Solution 1 (nlogn):

     1 public class Solution {
     2     /**
     3      * @param nums: The integer array
     4      * @return: The length of LIS (longest increasing subsequence)
     5      */
     6     public int longestIncreasingSubsequence(int[] nums) {
     7         if (nums.length==0) return 0;
     8         int len = nums.length;
     9         int[] seqEnd = new int[len+1];
    10         seqEnd[1] = 0;
    11         int lisLen = 1;
    12         for (int i=1;i<len;i++){
    13             int pos = findPos(nums,seqEnd,lisLen,i);
    14             seqEnd[pos] = i;
    15             if (pos>lisLen) lisLen = pos;
    16         }
    17 
    18         return lisLen;
    19         
    20     }
    21 
    22     public int findPos(int[] nums, int[] seqEnd, int lisLen, int index){
    23         int start = 1;
    24         int end = lisLen;
    25         while (start<=end){
    26             int mid = (start+end)/2;
    27     
    28             if (nums[index] == nums[seqEnd[mid]]){
    29                 return mid;
    30             } else if (nums[index]>nums[seqEnd[mid]]){
    31                 start = mid+1;
    32             } else end = mid-1;
    33         }
    34         return start;
    35     }
    36 }

     Solution 2 (n^2 DP):

     1 public class Solution {
     2     /**
     3      * @param nums: The integer array
     4      * @return: The length of LIS (longest increasing subsequence)
     5      */
     6     public int longestIncreasingSubsequence(int[] nums) {
     7         if (nums.length==0) return 0;
     8         int len = nums.length;
     9         int[] lisLen = new int[len];
    10         lisLen[0] = 1;
    11         int maxLen = lisLen[0];
    12         for (int i=1;i<len;i++){
    13             lisLen[i]=1;
    14             for (int j=i-1;j>=0;j--)
    15                 if (nums[i]>=nums[j] && lisLen[i]<lisLen[j]+1)
    16                     lisLen[i] = lisLen[j]+1;
    17             if (maxLen<lisLen[i]) maxLen = lisLen[i];
    18         }
    19 
    20         return maxLen;
    21 
    22     }
    23 }
  • 相关阅读:
    第七届蓝桥杯javaB组真题解析-煤球数目(第一题)
    考生须知
    2016年12月1日
    蓝桥网试题 java 基础练习 矩形面积交
    蓝桥网试题 java 基础练习 矩阵乘法
    蓝桥网试题 java 基础练习 分解质因数
    蓝桥网试题 java 基础练习 字符串对比
    个人银行账户管理程序
    new和delete的三种形式详解
    C++的六个函数
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4190936.html
Copyright © 2011-2022 走看看