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 < 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 = 1;
        int dp[40001][2];
        dp[0][1] = dp[0][0] = 1;
        for(int i = 1 ; i < A.size() ; i++){
            if(A[i] > A[i-1]){
                dp[i][0] = dp[i-1][1] + 1;
                dp[i][1] = 1;

            }
            else if(A[i] < A[i-1]){
                dp[i][1] = dp[i-1][0] + 1;
                dp[i][0] = 1;
            }
            else{
                dp[i][1] = 1;
                dp[i][0] = 1;
            }
            ans = max(ans,max(dp[i][0],dp[i][1]));
        }
        return ans;
    }
};

那么,换个思路,如果求的是最长的波动序列呢(可不连续)?

改下DP就行了,看下面代码:

if(a[i]>a[i-1]){
        dp[i][0]=max(dp[i-1][0],dp[i-1][1]+1);
        dp[i][1]=dp[i-1][1];
}
else if(a[i]<a[i-1]){
        dp[i][1]=max(dp[i-1][1],dp[i-1][0]+1);
        dp[i][0]=dp[i-1][0];
}
else if(a[i]==a[i-1]){
        dp[i][0]=dp[i-1][0];
        dp[i][1]=dp[i-1][1];
}return max(dp[n][0],dp[n][1]);

不是很难理解,递推下来不满足的不是等于1,而是等于上个状态取到的最长的。

以上。

原文地址:https://www.cnblogs.com/Esquecer/p/10311917.html

时间: 2024-07-30 23:45:06

leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)的相关文章

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] when k is odd, and A[k] < A[k+1] when k is even; OR, for i <

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

674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). 每日一算法2019/5/21Day 18LeetCode674. Longest Conti

ALBB 找公共最长连续字母序列的长度

问题描写叙述 给定一个 query 和一个 text .均由小写字母组成.要求在 text 中找出以相同的顺序连续出如今 query 中的最长连续字母序列的长度. 比如, query为"acbac".text为"acaccbabb",那么text中的"cba"为最长的连续出如今query中的字母序列,因此,返回结果应该为其长度3.请注意程序效率. 代码思想 1.遍历两字符串的每个元素,遇见同样元素则计算该次同样次数同样元素数目.并与之前最大值比較

最长连续字母序列的长度(阿里2015在线研发工程师笔试题)

给定一个query和一个text,均由小写字母组成.要求在text中找出以同样的顺序连续出现在query中的最长连续字母序列的长度.例如, query为“acbac”,text为“acaccbabb”,那么text中的“cba”为最长的连续出现在query中的字母序列,因此,返回结果应该为其长度3.请注意程序效率. 直接暴力,时间复杂度:m*n*n int Solve(char qu[],int n,char te[],int m) { int i,j; int k,p; int Max; in

LeetCode - 最长连续递增序列

题目描述: 给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3.尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开. 示例 2: 输入: [2,2,2,2,2] 输出: 1 解释: 最长连续递增序列是 [2], 长度为1. code: public class Solution { public int findLengthOfLCIS(i

得到最长连续递增序列

今天作死,看到别人发出来的笔试题就开干了,这tmd还理解错题目了,连续递增序列理解成上一个=下一个-1了. 这是我的成果,撸了4个多小时的: public class Test12 { public static void main(String[] args){ /** * 需求:找出最长的连续递增序列 * 步骤: * 1.找出所有连续序列可能结果,删除不是连续递增序列的,加入集合 * 2.集合排序,取第一个 * * 方式2: * 0.默认len为数组长度 * 1.找出数组中长度为len的序列

Codevs 2188 最长上升子序列 - 序列DP

传送门 题目描述 Description LIS问题是最经典的动态规划基础问题之一.如果要求一个满足一定条件的最长上升子序列,你还能解决吗? 给出一个长度为N整数序列,请求出它的包含第K个元素的最长上升子序列. 例如:对于长度为6的序列<2,7,3,4,8,5>,它的最长上升子序列为<2,3,4,5>,但如果限制一定要包含第2个元素,那么满足此要求的最长上升子序列就只能是<2,7,8>了. 输入描述 Input Description 第一行为两个整数N,K,如上所述.

LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)

题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. Even tho

[Leetcode] Longest consecutive sequence 最长连续序列

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4. Your algorithm should run in O(n