传送门:点我

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 < jA[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even;
  • OR, for i <= k < jA[k] > A[k+1] when k is even, and A[k] < A[k+1] when k 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. 1 <= A.length <= 40000
  2. 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 滚动数组)的更多相关文章

  1. 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...

  2. LeetCode 978. Longest Turbulent Subarray

    原题链接在这里:https://leetcode.com/problems/longest-turbulent-subarray/ 题目: A subarray A[i], A[i+1], ..., ...

  3. 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 ...

  4. [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  5. LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18

    674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given ...

  6. C#LeetCode刷题之#674-最长连续递增序列( Longest Continuous Increasing Subsequence)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3734 访问. 给定一个未经排序的整数数组,找到最长且连续的的递增 ...

  7. LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  8. [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  9. [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

随机推荐

  1. leetcode984

    public class Solution { private string M1(int A, int B) { StringBuilder sb = new StringBuilder(); ; ...

  2. ip黑白名单防火墙frdev的原理与实现

    汤之盘铭曰 苟日新 日日新 又日新 康诰曰 作新民 诗曰 周虽旧邦 其命维新 是故 君子无所不用其极 ——礼记·大学 在上一篇文章<DDoS攻防战 (二) :CC攻击工具实现与防御理论>中 ...

  3. 如何在notepad++实现代码自动化排版(调用Astyle)

    我先介绍这个怎么在notepad++中调用原版的astyle的方法. 在notepad++:运行或是F5, 在输入框中选择astyle.exe所在的目录,什么你没有astyle,下载地址https:/ ...

  4. PHP中Notice: unserialize(): Error at offset of bytes in on line 的解决方法

    使用unserialize函数将数据储存到数据库的时候遇到了这个报错,后来发现是将gb2312转换成utf-8格式之后,每个中文的字节数从2个增加到3个之后导致了反序列化的时候判断字符长度出现了问题, ...

  5. keyword模块

    导入关键字模块 import keyword 列出当前系统中Python的关键字 >>> keyword.kwlist ['and', 'as', 'assert', 'break' ...

  6. day07-while,for循环

    1.循环语句 Python提供了for循环和while循环while在给定的判断条件为 true 时执行循环体,否则退出循环体.for重复执行语句嵌套循环可以在while循环体中嵌套for.while ...

  7. Python 百分号字符串拼接

    # %s可以接收一切 %d只能接收数字 msg = 'i am %s my hobby is %s' %('lhf','alex') print msg msg2 = 'i am %s my hobb ...

  8. css-选择器性能

    ID选择器 比如#header 类选择器 比如.promo 元素选择器 比如 div 兄弟选择器 比如 h2 + p 子选择器 比如 li > ul 后代选择器 比如 ul a 7. 通用选择器 ...

  9. 尚硅谷springboot学习17-SpringBoot日志

    SpringBoot使用它来做日志功能: <dependency> <groupId>org.springframework.boot</groupId> < ...

  10. 数据结构:Queue

    Queue设计与实现 Queue基本概念 队列是一种特殊的线性表 队列仅在线性表的两端进行操作 队头(Front):取出数据元素的一端 队尾(Rear):插入数据元素的一端 队列不允许在中间部位进行操 ...