您的位置:首页 > 其它

lintCode刷题--最长上升连续子序列

2015-11-24 17:13 246 查看
题目:


最长上升连续子序列

27%

通过

给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列。(最长上升连续子序列可以定义为从右到左或从左到右的序列。)

您在真实的面试中是否遇到过这个题? 

Yes

样例

给定 
[5, 4, 2, 1, 3]
, 其最长上升连续子序列(LICS)为 
[5,
4, 2, 1]
, 返回 
4
.
给定 
[5, 1, 2, 3, 4]
, 其最长上升连续子序列(LICS)为 
[1,
2, 3, 4]
, 返回 
4
.

注意

time

标签 Expand 

动态规划 数组

相关题目 Expand 

困难最长上升连续子序列
II

解答:

求出最长上升的子序列的长度,len1

求出最长下降的子序列,长度len2,返回 max(len1, len2);

代码:

class Solution {

    /**

     * @param dictionary: an array of strings

     * @return: an arraylist of strings

     */

    ArrayList<String> longestWords(String[] dictionary) {

        // write your code here

        if(dictionary == null)

            return null;

        String[] dic = dictionary;

         ArrayList<String> array = new  ArrayList<String>();

         array.add(dic[0]);

        int len = dic.length;

        int longest =  dic[0].length();;

        int i =1;

        int lenTmp = -1;

        while(i < len){

            

            lenTmp = dic[i].length();

            if(longest < lenTmp){

                longest = lenTmp;

                array =
a7a3
new  ArrayList<String>();

                array.add(dic[i]);

            }else if(longest == lenTmp){

                array.add(dic[i]);

            }

            i++;

        }

        return array;

        

    }

};

变体:改为求最长连续上升(或者下降)子序列

解法:记录上升序列的起始位置、结束位置

public class Solution{

public static void main(String[] args) throws IOException{

int[] array = {99,55,7,29,80,33,19,23,6,
  35,40,27,44,74,5,17,52,36,
  67,32,37,42,18,77,66,62,97,
  79,60,94,30,2,85,22,26,91,
  3,16,8,0,48,93,39,31,63,13,
  71,58,69,50,21,70,61,43,12,
  88,47,45,72,76};//{5,1,2,3,4};

int[] max= longestIncreasingContinuousSubsequence(array);
printf(max);

 

}

private static void printf(int[] max) {
// TODO Auto-generated method stub
if(max == null)
printf("null");
for(int i=0; i< max.length;i++)
System.out.printf("%d ", max[i]);
printf("");

}

public static int[] longestIncreasingContinuousSubsequence(int[] A) {

    // Write your code here

    int[] max1 = longestIncreasingContinuousSubsequence(A,false);

    int[] max2 = longestIncreasingContinuousSubsequence(A,true);

    return max1.length>max2.length?max1:max2;

}

public static int[] longestIncreasingContinuousSubsequence(int[] A, boolean flag){

    if(A==null)

        return null;

    int len = A.length;

    if(len == 0)

        return null;

    int max =0;

    int tmp = 1;

    int first = 1,sencond =1;

    int start = -1,maxStart = -1;

    int end = -1, maxEnd = -1;

    if(flag == true){// 5, 1, 2, 3, 4

        for(int i =0; i<len - 1; i++){

            if(A[i] < A[i+1]){

            if(first == 1){

            start = i;

            first=0;

            }

                tmp++;

                

            }else{

            if(first == 0){

            first = 1;

            end = i;

            }

                if(max < tmp){

                    max = tmp;

                    maxStart = start;

                    maxEnd = end;

                   

                }

                tmp=1;

            }

        }

       if(max < tmp){

      max = tmp;

      maxStart = start;

      maxEnd = end;

     

       }

    }else{

    for(int i =0; i<len - 1; i++){

             if(A[i] > A[i+1]){

              if(first == 1){

              start = i;

              first =0;

              }

                 tmp++;

             }else{

              if(first == 0){

              first = 1;

              end = i;

              }

                 if(max < tmp){

                     max = tmp;

                     maxStart = start;

                     maxEnd = end;

                 }

                 tmp=1;

             }

         }

        if(max < tmp){

        max = tmp;

        maxStart = start;

        maxEnd = end;

        }

    }

    int[] result = new int[maxEnd - maxStart+1];

    if(result.length!=max){

    printf("error!");

    }

    for(int i = 0; i < max;i++){

    result[i] = A[i+maxStart];

    }

    return result;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: