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 };
找我内推: 字节跳动各种岗位
作者:
ZH奶酪(张贺)
邮箱:
cheesezh@qq.com
出处:
http://www.cnblogs.com/CheeseZH/
*
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。