LeetCode--978--medium--MostTurbulentSubarray

package com.app.main.LeetCode.slidingwindow;

/**
 *
 * 978
 *
 * medium
 *
 * 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 <= k < j, A[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 <= A.length <= 40000
 * 0 <= A[i] <= 10^9
 *
 *
 * Created with IDEA
 * author:Dingsheng Huang
 * Date:2020/1/21
 * Time:下午3:09
 */
public class MostTurbulentSubarray {

    // [9,4,2,2,7,8,8,1,9]
    // [9,4,2,10,7,8,8,1,9]
    // dp 1
    public int maxTurbulenceSize(int[] A) {

        if (A.length == 1) {
            return 1;
        }
        int ans = 1;
        boolean pre = true;
        int[] dp = new int[A.length];
        dp[0] = 1;

        boolean init = true;
        for (int i = 1; i < A.length; i++) {
            while (i < A.length && A[i] == A[i - 1]) {
                dp[i] = 1;
                i++;
                init = true;
            }
            if (i == A.length) {
                break;
            }
            if (A[i] > A[i - 1]) {
                if (!pre || init) {
                    dp[i] = dp[i - 1] + 1;
                    pre = true;
                    init = false;
                } else {
                    dp[i] = 2;
                }
            } else {
                if (pre || init) {
                    dp[i] = dp[i - 1] + 1;
                    pre = false;
                    init = false;
                } else {
                    dp[i] = 2;
                }
            }
            ans = Math.max(ans, dp[i]);
        }
        return ans;
    }

    // dp 2
    public int maxTurbulenceSize2(int[] A){
        if (A.length == 0 || A == null) {
            return 0;
        }
        if (A.length == 1) {
            return 1;
        }
        int ans = 1;
        int greater = 1;
        int less = 1;
        for (int i = 1; i < A.length; i++) {
            if (A[i] > A[i - 1]) {
                greater = less + 1;
                less = 1;
                ans = Math.max(ans, greater);
            } else if (A[i] < A[i - 1]) {
                less = greater + 1;
                greater = 1;
                ans = Math.max(ans, less);
            } else {
                greater = 1;
                less = 1;
            }
            // optimize , pruning needn't compare
            // ans = Math.max(ans, Math.max(greater, less));
        }
        return ans;
    }
}
发布了187 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/huangdingsheng/article/details/104306081
今日推荐