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 ...
随机推荐
- 一次ssh远程不能登录的排查
原创文件,欢迎阅读,禁止转载. 今天发现一台主机不能远程了,ssh连接不上了. 排查过程是这样的:1. ping没问题. 2. 通过telnet看端口是否开启.[user@localhost ~]$ ...
- Activity工作流学习(二)--Activity数据库
23张表 ACT_RE_资源库流程规划表 act_re_deployment 部署信息表 act_re_model 流程设计模型部署表 act_re_procdef 流程定义数据表 ACT_RU_运行 ...
- db连接驱动
1.oracle 驱动jar包-->ojdbc6.jar 驱动类-->oracle.jdbc.driver.OracleDriver 驱动连接--> 第一种:jdbc:oracle: ...
- Druid参考配置
pom中的maven dependency <dependency> <groupId>com.alibaba</groupId> ...
- netty为啥要二次开发
很早之前就看过李林峰写的netty的书,但是感觉没有直接用到还是理解不够深入,现在的公司有两套自己基于Netty开发的系统,感觉才真正理解为啥要这么做 借用别人文章回顾下 https://www.cn ...
- vue.js 初级之一
vue.js 是一个构建数据驱动的 web 界面 渐进式驱动框架. 引用的话,直接使用script标签引入就可以了: <script src="./lib/vue.js"&g ...
- javascript:解决两个小数相乘出现无限小数
两个小数相乘,会出现无限小数:先把小数乘以10或100或1000(小数点后有多少位就乘以多少),再相乘,最后再除以10或100或1000
- sqoop连接SqlServer2012示例
sqoop import --connect 'jdbc:sqlserver://192.168.xx.xx:1433;username=sa;password=xxxx;database=WindE ...
- How to Pronounce TH after N or Z
How to Pronounce TH after N or Z Share Tweet Share Tagged With: Linking Consonant to Consonant The T ...
- Python调用外部系统命令
利用Python调用外部系统命令的方法可以提高编码效率.调用外部系统命令完成后可以通过获取命令执行返回结果码.执行的输出结果进行进一步的处理.本文主要描述Python常见的调用外部系统命令的方法,包括 ...