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 }
  • 相关阅读:
    内存寻址:逻辑地址到物理地址的转化
    变量类型,变量作用域,变量存储空间,变量生命周期
    位运算计算与位运算应用
    sizeof()计算
    位域(位段)
    自然对齐和强制对齐
    内存中的数据对齐
    用汇编编写子程序,可以显示字符串到屏幕指定位置
    汇编语言 实验9 根据材料编程
    80x25彩色字符模式
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4190936.html
Copyright © 2011-2022 走看看