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 }
  • 相关阅读:
    安全规约
    课时作业1
    C# 操作防火墙 个人类库
    解决WinScp连接被拒绝的问题
    C# 使用WinSCP方法 类库、脚本
    【运维知识】BAT处理 延迟启动程序 临时解决网络IP获取慢导致的网络连接失败
    AngularJS入门教程之与服务器(Ajax)交互操作示例【附完整demo源码下载】
    用Angular实时获取本地Localstorage数据,实现一个模拟后台数据登入的效果
    AngularJS实现ajax请求的方法
    AngularJS中指令的四种基本形式实例分析
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4190936.html
Copyright © 2011-2022 走看看