• 941. Valid Mountain Array


    package LeetCode_941
    
    /**
     * 941. Valid Mountain Array
     * https://leetcode.com/problems/valid-mountain-array/description/
     *
     * Given an array A of integers, return true if and only if it is a valid mountain array.
    Recall that A is a mountain array if and only if:
    A.length >= 3
    There exists some i with 0 < i < A.length - 1 such that:
    A[0] < A[1] < ... A[i-1] < A[i]
    A[i] > A[i+1] > ... > A[A.length - 1]
     * */
    class Solution {
        fun validMountainArray(A: IntArray): Boolean {
            if (A.size < 3) {
                return false
            }
            /*
            * -has_down happened before has_up
              -not has_down or not has_up
            * */
            val n = A.size
            var hasDown = false
            var hasUp = false
            for (i in 0 until n - 1) {
                if (A[i] < A[i + 1]) {//is going up
                    if (hasDown) {
                        return false
                    }
                    hasUp = true
                } else if (A[i] > A[i + 1]) {//is going down
                    //if hasDown happened before hasUp
                    if (!hasUp) {
                        return false
                    }
                    hasDown = true
                } else {
                    //can not be equal
                    return false
                }
            }
            return hasUp && hasDown
        }
    }
  • 相关阅读:
    非旋Treap——fhq treap
    LCA
    树链剖分
    复习计划
    BZOJ2565: 最长双回文串(回文树)
    回文自动机
    luogu P3796 【模板】AC自动机(加强版)
    【BZOJ2908】 又是nand
    【HDU2460】 Network
    【CF786B】 Legacy
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13127053.html
Copyright © 2020-2023  润新知