题解 | #牛牛的数列#
牛牛的数列
http://www.nowcoder.com/practice/f2419f68541d499f920eac51c63d3f72
双向dp
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param nums intvector
* @return int
*/
int maxSubArrayLength(vector<int>& nums) {
// write code here
// [7,2,3,1,5,6]
// 1 1 2 1 2 3
// 1 2 1 3 2 1
int m = nums.size();
if (m < 3) {
return m;
}
vector<int> left(m, 1), right(m, 1);
for (int i = 1; i < m; i++) {
if (nums[i] > nums[i - 1]) {
left[i] = left[i - 1] + 1;
}
}
for (int i = m - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
right[i] = right[i + 1] + 1;
}
}
int ans = 1;
for (int i = 1; i < m - 1; i++) {
if (nums[i + 1] - nums[i - 1] > 1) {
ans = max(ans, left[i - 1] + right[i + 1] + 1);
}
}
return ans;
}
};