LeetCode 978. Longest Turbulent Subarray
原题链接在这里:https://leetcode.com/problems/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
题解:
Set some small examples like [1, 3, 2], [2,2] and find routine.
It matters the last 3 componenets. If it is l<m>r or l>m<r relationship, then length+1. Otherwise, reset to 2 or 1.
Let dp[i] denotes up to A[i-1], the longest turbulent length.
If A[i-3]<A[i-2]>A[i-1] or A[i-3]>A[i-2]<A[i-1], dp[i] = dp[i-1] + 1.
Maintain the maximum to res.
Time Complexity: O(n). n = A.length.
Space: O(n).
AC Java:
class Solution {
public int maxTurbulenceSize(int[] A) {
if(A == null){
return 0;
}
if(A.length < 2){
return A.length;
}
int len = A.length;
int [] dp = new int[len+1];
dp[1] = 1;
dp[2] = A[0] == A[1] ? 1 : 2;
int res = dp[2];
for(int i = 3; i<=len; i++){
if(A[i-2]<A[i-3] && A[i-2]<A[i-1] || A[i-2]>A[i-3] && A[i-2]>A[i-1]){
dp[i] = dp[i-1] + 1;
res = Math.max(res, dp[i]);
}else if(A[i-1] == A[i-2]){
dp[i] = 1;
}else{
dp[i] = 2;
}
}
return res;
}
}
It only cares about dp[i-1]. Thus it could reduce dimension.
Time Complexity: O(n).
Space: O(1).
AC Java:
class Solution {
public int maxTurbulenceSize(int[] A) {
if(A == null){
return 0;
}
if(A.length < 2){
return A.length;
}
int len = A.length;
int dp = A[0] == A[1] ? 1 : 2;
int res = dp;
for(int i = 3; i<=len; i++){
if(A[i-2]<A[i-3] && A[i-2]<A[i-1] || A[i-2]>A[i-3] && A[i-2]>A[i-1]){
dp = dp + 1;
res = Math.max(res, dp);
}else if(A[i-1] == A[i-2]){
dp = 1;
}else{
dp = 2;
}
}
return res;
}
}
LeetCode 978. Longest Turbulent Subarray的更多相关文章
- 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 turbule ...
- 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...
- 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]LeetCode978. 最长湍流子数组 | 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 ...
- Longest Turbulent Subarray LT978
A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
Given an array of integers nums and an integer limit, return the size of the longest continuous suba ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
随机推荐
- APIO2019题解
T1.桥梁(bridges/restriction) Subtask1:暴力,$O(n^2)$. #include<cstdio> #include<algorithm> #d ...
- 题解 POJ 2559-SP1805 【HISTOGRA - Largest Rectangle in a Histogram】
题目链接: https://www.luogu.org/problemnew/show/SP1805 http://poj.org/problem?id=2559 思路: ## 单调栈 首先如果所有矩 ...
- 解决vue-cli项目开发中跨域问题
一.开发环境中跨域 使用 Vue-cli 创建的项目,开发地址是 localhost:8080,需要访问非本机上的接口http://10.1.0.34:8000/queryRole.不同域名之间的访问 ...
- springCloud学习笔记2(服务发现)
本篇代码存放于:https://github.com/FleyX/demo-project/tree/master/springcloud/spring-cloud%E6%9C%8D%E5%8A%A1 ...
- margin 外边距合并问题
一.兄弟元素的外边距合并 效果图如下:(二者之间的间距为100px,不是150px) 二.嵌套元素的外边距合并 对于两个嵌套关系的元素,如果父元素中没有内容或者内容在子元素的后面并且没有上内边距及边框 ...
- java系统化基础-day01-基础语法知识
1.学前必看 该课程将系统化的讲解java基础,但是该课程并不适合零基础的学员,因为在整个java学习体系中我们是按照实际生产设计, 主体思路是以完成某个业务为主线,用到什么技术就学什么技术,即带着问 ...
- JavaScript: 数据类型检测
由于JavaScript是门松散类型语言,定义变量时没有类型标识信息,并且在运行期可以动态更改其类型,所以一个变量的类型在运行期是不可预测的,因此,数据类型检测在开发当中就成为一个必须要了解和掌握的知 ...
- 转一篇关于epoll模型的博文
以前就看过这篇关于epoll文章,现在又翻出来看了一下,很久不看的知识真是容易忘啊. 原文出处: http://blog.163.com/huchengsz@126/blog/static/73483 ...
- MSP430 LaunchPad开发板入门教程集合
MSP-EXP430G2开发板是德州仪器提供的开发工具,也称为LaunchPad,用于学习和练习如何使用其微控制器产品.该开发板属于MSP430 Value Line系列,我们可以对所有MSP430系 ...
- CentOS7安装Chrome
1. 进入官网:https://www.google.cn/intl/zh-CN/chrome/2. 点击下载3. 直接安装:sudo yum localinstall google-chrome-s ...