lintcode-easy-Longest Increasing Continuous Subsequence

Give an integer array,find the longest increasing continuous subsequence in this array.

An increasing continuous subsequence:

  • Can be from right to left or from left to right.
  • Indices of the integers in the subsequence should be continuous.

Example

For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.

For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.

Notice

O(n) time and O(1) extra space.

public class Solution {
    /**
     * @param A an array of Integer
     * @return  an integer
     */
    public int longestIncreasingContinuousSubsequence(int[] A) {
        // Write your code here
        if(A == null || A.length == 0)
            return 0;

        if(A.length == 1)
            return 1;

        int result = 1;
        int count = 1;

        for(int i = 1; i < A.length; i++){
            if(A[i] > A[i - 1])
                count++;
            else
                count = 1;
            if(count > result)
                result = count;
        }

        count = 1;

        for(int i = A.length - 2; i >= 0; i--){
            if(A[i] > A[i + 1])
                count++;
            else
                count = 1;
            if(count > result)
                result = count;
        }

        return result;
    }
}
时间: 2024-08-24 11:05:34

lintcode-easy-Longest Increasing Continuous Subsequence的相关文章

[LintCode] Longest Increasing Continuous Subsequence

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Example Fo

[LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Notice O(n

Longest Increasing Continuous subsequence II

Give you an integer matrix (with row size n, column size m),find the longest increasing continuous subsequence in this matrix. (The definition of the longest increasing continuous subsequence here can start at any row or column and go up/down/right/l

Longest Increasing Continuous Subsequence

Give an integer array,find the longest increasing continuous subsequence in this array. An increasing continuous subsequence: Can be from right to left or from left to right. Indices of the integers in the subsequence should be continuous. Notice O(n

LintCode &quot;Longest Increasing Continuous subsequence II&quot; !!

DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vector<int>>& A, vector<vector<int>>& dp) { if(dp[i][j] != 0) return dp[i][j]; if (i > 0 && A[i-1][j] > A[i][j]) { dp

lintcode 容易题:Longest Increasing Continuous subsequence 最长上升连续子序列

题目: 最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 样例 给定 [5, 4, 2, 1, 3], 其最长上升连续子序列(LICS)为 [5, 4, 2, 1], 返回 4. 给定 [5, 1, 2, 3, 4], 其最长上升连续子序列(LICS)为 [1, 2, 3, 4], 返回 4. 解题: 这个直接找就可以了,最长的升序,和最长的降序,再求最大值,时

Longest Increasing Common Subsequence (LICS)

最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1..m]的LICS的长度, a[], b[]数组的元素均为正整数. Solution 考虑如何定义DP状态,定义DP状态就是定义所谓的最优子问题(optimal subproblem),而DP状态要能转移,就是所谓最优子问题要具有重叠子结构. 将DP状态定义为 DP[i][j]:a[1..i], b[

[LintCode]76. 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 [2, 4, 5, 7], return 4 Challenge

[Lintcode easy]Longest Words

Longest Words Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog", "google", "facebook", "internationalization", "blabla" } the longest words are(is) ["internati