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[B.length - 1]

Example 1:

Input: [2,1]
Output: false

Example 2:

Input: [3,5,5]
Output: false

Example 3:

Input: [0,3,2,1]
Output: true

Note:

  1. 0 <= A.length <= 10000
  2. 0 <= A[i] <= 10000 

Approach #1:

class Solution {
public:
bool validMountainArray(vector<int>& A) {
int len = A.size();
if (len < 3) return false;
auto index = max_element(A.begin(), A.end()) - A.begin();
if (index == len - 1 || index == 0) return false;
for (int i = 0; i < index; ++i)
if (A[i] >= A[i+1]) return false;
for (int i = index; i < len-1; ++i)
if (A[i] <= A[i+1]) return false;
return true;
}
};

  

Weekly Contest 111-------->941. Valid Mountain Array(max_element)的更多相关文章

  1. 【Leetcode_easy】941. Valid Mountain Array

    problem 941. Valid Mountain Array solution: class Solution { public: bool validMountainArray(vector& ...

  2. 【leetcode】941. Valid Mountain Array

    题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...

  3. 【LeetCode】941. Valid Mountain Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. LeetCode 941. Valid Mountain Array (有效的山脉数组)

    题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...

  5. LeetCode 941. 有效的山脉数组(Valid Mountain Array)

    941. 有效的山脉数组 941. Valid Mountain Array 题目描述 给定一个整数数组 A,如果它是有效的山脉数组就返回 true,否则返回 false. 让我们回顾一下,如果 A ...

  6. [Swift]LeetCode941. 有效的山脉数组 | Valid Mountain Array

    Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...

  7. Valid Mountain Array LT941

    Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...

  8. 112th LeetCode Weekly Contest Minimum Increment to Make Array Unique

    Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1. Return ...

  9. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

随机推荐

  1. c结构体里的数组与指针

    /* 訪问成员数组名事实上得到的是数组的相对地址.而訪问成员指针事实上是相对地址里的内容 */ struct buf_str { int length; char buf[0]; }; struct ...

  2. 自译Solr in action中文版

    文件夹 Part 1 初识 SOLR 1 Solr 简单介绍 2 開始熟悉 Solr 3 Solr 核心概念 4 配置 Solr 5 建立索引 6 文本分析 Part 2 Solr 核心功能 7 发起 ...

  3. MySQL 数据类型转换

    版权个人所有,欢迎转载如转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5505523.html 网络越来越达到所以带来的好处不容置疑. ...

  4. 2015年度新增开源软件排名TOP 100,EasyDarwin开源流媒体服务器排名第17

    本榜单包含 2015 年开源中国新收录的 5977 款开源软件中,根据软件本身的关注度.活跃程度进行排名前 100 名的软件.从这份榜单中或许可以了解到最新业界的趋势. 榜单详情:http://www ...

  5. bootstrap-table 行内编辑

    1.文件引入 <link rel="stylesheet" href="bootstrap.css"> <link rel="sty ...

  6. yuicompressor

      yui/yuicompressor: YUI Compressor https://github.com/yui/yuicompressor    YUI Compressor 详细介绍 YUI ...

  7. Chef vs Puppet vs Ansible vs Saltstack: Which Works Best For You?

    Ansible vs SaltStack 谁才是自动化运维好帮手? - CSDN博客 https://blog.csdn.net/a105421548/article/details/53558598 ...

  8. ReentrantLock(重入锁)简单源码分析

    1.ReentrantLock是基于AQS实现的一种重入锁. 2.先介绍下公平锁/非公平锁 公平锁 公平锁是指多个线程按照申请锁的顺序来获取锁. 非公平锁 非公平锁是指多个线程获取锁的顺序并不是按照申 ...

  9. Android笔记之获取显示器宽高

    原先的Display.getWidth().Display.getHeight()已废弃 推荐的获取Display宽高的方法如下 DisplayMetrics metrics = new Displa ...

  10. Template Pattern

    1.Template模式解决的问题:对于某一个业务逻辑在不同的对象中有不同的细节实现,但是逻辑的框架是相同的.将逻辑框架放在抽象基类中,并定义好细节的接口,子类中实现细节.Template模式利用多态 ...