码迷,mamicode.com
首页 > 其他好文 > 详细

LintCode: Longest Increasing Continuous subsequence

时间:2015-11-30 13:13:50      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

LintCode: Longest Increasing Continuous subsequence

标签:

原文地址:http://www.cnblogs.com/CheeseZH/p/5006811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!