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) time and O(1) extra space.

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.

Runtime: 29ms

 1 class Solution {
 2 public:
 3     /**
 4      * @param A an array of Integer
 5      * @return  an integer
 6      */
 7     int longestIncreasingContinuousSubsequence(vector<int>& A) {
 8         // Write your code here
 9
10         if (A.empty()) return 0;
11         int result = 1;
12         int leftMax = 1, rightMax = 1;
13         for (int i = 1; i < A.size(); i++) {
14             // from left to right
15             if (A[i] > A[i - 1]) {
16                 leftMax++;
17                 rightMax = 1;
18                 result = max(result, leftMax);
19             }
20             else { // from right to left
21                 leftMax = 1;
22                 rightMax++;
23                 result = max(result, rightMax);
24             }
25         }
26         return result;
27     }
28 };

Runtime: 838ms

 1 class Solution {
 2 public:
 3     /**
 4      * @param A an array of Integer
 5      * @return  an integer
 6      */
 7     int longestIncreasingContinuousSubsequence(vector<int>& A) {
 8         // Write your code here
 9         if (A.empty()) return 0;
10
11         // scan from left to right
12         int leftMax = 1;
13         for (int i = 1; i < A.size(); i++) {
14             int j = i;
15             while (j < A.size() && A[j] > A[j - 1]) j++;
16             leftMax = max(leftMax, j - i + 1);
17         }
18
19         // scan from right to left
20         int rightMax = 1;
21         for (int i = A.size() - 2; i >= 0; i--) {
22             int j = i;
23             while (j >= 0 && A[j] > A[j + 1]) j--;
24             rightMax = max(i - j + 1, rightMax);
25         }
26         return max(leftMax, rightMax);
27     }
28 };
时间: 2024-08-26 03:30:12

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

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. 解题: 这个直接找就可以了,最长的升序,和最长的降序,再求最大值,时

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 Fo

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[

LeetCode Number of Longest Increasing Subsequence

原题链接在这里:https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/ 题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longe

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu