leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)
传送门:点我
978. Longest Turbulent Subarray
A subarray A[i], A[i+1], ..., A[j]
of A
is said to be turbulent if and only if:
- For
i <= k < j
,A[k] > A[k+1]
whenk
is odd, andA[k] < A[k+1]
whenk
is even; - OR, for
i <= k < j
,A[k] > A[k+1]
whenk
is even, andA[k] < A[k+1]
whenk
is odd.
That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
Return the length of a maximum size turbulent subarray of A.
Example 1:
Input: [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: (A[1] > A[2] < A[3] > A[4] < A[5])
Example 2:
Input: [4,8,12,16]
Output: 2
Example 3:
Input: [100]
Output: 1
Note:
1 <= A.length <= 40000
0 <= A[i] <= 10^9
题意:求最长的连续波动子序列,注意是连续。
思路:DP滚动一下就行了。
class Solution {
public:
int maxTurbulenceSize(vector<int>& A) {
int ans = ;
int dp[][];
dp[][] = dp[][] = ;
for(int i = ; i < A.size() ; i++){
if(A[i] > A[i-]){
dp[i][] = dp[i-][] + ;
dp[i][] = ; }
else if(A[i] < A[i-]){
dp[i][] = dp[i-][] + ;
dp[i][] = ;
}
else{
dp[i][] = ;
dp[i][] = ;
}
ans = max(ans,max(dp[i][],dp[i][]));
}
return ans;
}
};
那么,换个思路,如果求的是最长的波动序列呢(可不连续)?
改下DP就行了,看下面代码:
if(a[i]>a[i-]){
dp[i][]=max(dp[i-][],dp[i-][]+);
dp[i][]=dp[i-][];
}
else if(a[i]<a[i-]){
dp[i][]=max(dp[i-][],dp[i-][]+);
dp[i][]=dp[i-][];
}
else if(a[i]==a[i-]){
dp[i][]=dp[i-][];
dp[i][]=dp[i-][];
}
return max(dp[n][0],dp[n][1]);
不是很难理解,递推下来不满足的不是等于1,而是等于上个状态取到的最长的。
以上。
leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)的更多相关文章
- 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...
- LeetCode 978. Longest Turbulent Subarray
原题链接在这里:https://leetcode.com/problems/longest-turbulent-subarray/ 题目: A subarray A[i], A[i+1], ..., ...
- 978. Longest Turbulent Subarray
A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18
674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given ...
- C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
随机推荐
- php7内核执行流程(转载留记录)
- 机器学习进阶-阈值与平滑-图像平滑操作(去噪操作) 1. cv2.blur(均值滤波) 2.cv2.boxfilter(方框滤波) 3. cv2.Guassiannblur(进行高斯滤波) 4. cv2.medianBlur(进行中值滤波)
1.cv2.blur(img, (3, 3)) 进行均值滤波 参数说明:img表示输入的图片, (3, 3) 表示进行均值滤波的方框大小 2. cv2.boxfilter(img, -1, (3, ...
- 伪AJAX
<h3>3,伪ajax</h3> <h6>学习iframe(嵌套别人家网站的)</h6> <div> <input id=" ...
- 编译Linux内核(Mac OS平台)
操作系统第一次实验需要编译Linux内核,我之前在Mac上一直使用的都是Parallels Desktop这个软件,所以这次也将课程网站上提供的Ubuntu安装在了PD上,但是编译完内核后无法进入Ub ...
- 设置git的http代理
如果git仓库不和本地代码之间不可以直达,这个时候就可以考虑使用git 代理的方式提交代码到git仓库了; git http代理或者https代理,配置在~/.gitconfig 文件下,可以直接编辑 ...
- 初探 objc_msgSend函数
1.0 执行某个对象的方法 [receiver message] 被编译为: id objc_msgSend(id self,SEL op,...): objc_msgSend 发送信息的过程 ...
- 利用monkey测试android,入门级用户可能遇见的错误及解决办法 【转】
转自[http://blog.csdn.net/zm2714/article/details/7977930] 一.D:\android\android-sdk-windows\tools目录中没有a ...
- 我在eclipse输出的第一个hello world!
下学期就要学习JAVA 语言,我现在对它好像还真的是一无所知.记得两次在帮学长做测评的时候,他们都说要装上eclipse.然后从放假我就忙着下载,安装,但是由于官网都是英文,似乎一直在出差错.询问了学 ...
- ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法
原文链接:https://www.cnblogs.com/kaid/p/8616385.html 在ubuntu系统的termial下,用apt-get install 安装软件的时候,如果在未完成下 ...
- svn 更新lib库时,报错
svn: E195012: Unable to find repository location for svn:// in revision 9718 Can't revert without re ...