• 978. Longest Turbulent Subarray


    package LeetCode_978
    
    /**
     * 978. Longest Turbulent Subarray
     * https://leetcode.com/problems/longest-turbulent-subarray/description/
     *
     * 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])
     * */
    class Solution {
        /*
        * solution: Sliding Window, Time complexity:O(n), Space complexity:O(1)
        * */
        fun maxTurbulenceSize(A: IntArray): Int {
            if (A == null || A.isEmpty()) {
                return 0
            }
            if (A.size == 1) {
                return 1
            }
            val len = A.size
            var max = 0
            var left = 0
            var right = 1
            while (right < len) {
                if (isZigZag(A, left, right)) {
                    max = Math.max(max, right - left + 1)
                    right++
                    continue
                }
                //if the two number in left and right are equal
                //for example:[9,9]
                if (A[right] == A[right - 1]) {
                    left = right
                    max = Math.max(max, right - left + 1)
                    right++
                    continue
                }
                left = right - 1
                right++
            }
            return max
        }
    
        //4,2,10,7,8 is zigzag
        private fun isZigZag(array: IntArray, start: Int, end: Int): Boolean {
            if (array[start] == array[start + 1]) {
                return false
            }
            var increasing = array[start] < array[start + 1]
            for (i in start + 1 until end) {
                //turn it each time
                increasing = !increasing
                if (increasing && array[i] >= array[i + 1] || !increasing && array[i] <= array[i + 1]) {
                    return false
                }
            }
            return true
        }
    }
  • 相关阅读:
    Centos7.3安装和配置Tomcat8
    Centos7.3安装和配置jre1.8
    -bash: /home/java/jdk1.8.0_221/bin/java: 权限不够
    Centos7安装vsftp服务
    CENTOS之文件搜索命令Find
    Centos之文件搜索命令locate
    DockerFile体系结构(保留字指令)
    DockerFile解析
    SpringBoot项目映射
    穿透,让你不再限制内网的约束【远程控制内网电脑】【3389端口】
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13228046.html
Copyright © 2020-2023  润新知