LintCode: Longest Increasing Continuous subsequence

C++

双向递推

空间O(1)

时间O(n)

 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         int n = A.size();
10         if ( n == 1 || n == 0) {
11             return n;
12         }
13         int dp;
14         int ans = 1;
15         //>>>>>>>
16         dp = 1;
17         for (int i=1; i<=n; ++i) {
18             dp = A[i]>=A[i-1]?dp+1:1;
19             ans = max(dp, ans);
20         }
21         //<<<<<<<
22         dp = 1;
23         for (int i=n-2; i>=0; --i) {
24             dp = A[i]>=A[i+1]?dp+1:1;
25             ans = max(dp, ans);
26         }
27         return ans;
28     }
29 };

 

posted @ 2015-11-30 12:08  ZH奶酪  阅读(189)  评论(0编辑  收藏  举报