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]whenkis odd, andA[k] < A[k+1]whenkis even; - OR, for
i <= k < j,A[k] > A[k+1]whenkis even, andA[k] < A[k+1]whenkis 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 <= 400000 <= 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 ...
随机推荐
- react-native 集成react-native-image-crop-picker,使用相册相机功能
先是安装 和链接 npm i react-native-image-crop-picker --save react-native link react-native-image-crop-picke ...
- epoll_wait 时 POLLERR 与 POLLIN 同时返回的现象解析(转)
今天code review时,同事B对我代码中的poll()的处理做法提出了异议.于是做了些研究,还发现了一些好玩的故事. 异议的代码 我的代码是参考manpage写的,类似下面的做法.同事B说没有处 ...
- 尚硅谷springboot学习18-日志使用
默认配置 SpringBoot默认帮我们配置好了日志 //记录器 Logger logger = LoggerFactory.getLogger(getClass()); @Test public v ...
- C++复习:类和对象
类和对象 基本概念 1)类.对象.成员变量.成员函数 2)面向对象三大概念 封装.继承.多态 3)编程实践 类的定义和对象的定义,对象的使用 求圆形的面积 定义Teacher类 ...
- FlashDevelop 发布SWC
环境配置: 1.安装32位Java,经测试可用的 链接:http://pan.baidu.com/s/1i4CraXR 密码:xuud 2.ant,链接:http://pan.baidu.com/s/ ...
- linux 3.10中完成量的使用
完成量是基于等待队列设计的,所以显然不能在中断上下文使用完成量. struct completion { unsigned int done; wait_queue_head_t wait; }; 我 ...
- adb常用命令及详解
ADB 即 Android Debug Bridge,Android调试桥.ADB工作方式比较特殊,采用监听Socket TCP 端口的方式让IDE和Qemu通讯,默认情况下adb会daemon相关的 ...
- week5 0.1 安装materializecss
用ATOM打开项目 App是什么呢?就是App.js 我们将不需要的删掉 用一下materialize(类似bootstrap的东西) 官网https://materializecss.com/ 想用 ...
- 远程批量获取Linux和Windos操作系统版本(内核)
在不登录远程主机的情况下,可以查看远程主机的服务器操作系统版本(内核). 脚本执行前提: 1.拷贝check_snmp到脚本执行的主机中或在此主机中安装nagios; 2.保持list.txt中只有一 ...
- 吴裕雄 python 机器学习-NBYS(1)
import numpy as np def loadDataSet(): postingList=[['my', 'dog', 'has', 'flea', 'problems', 'help', ...