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. Mvc Autofac构造器注入

    新建MVC项目,添加程序集引用 定义接口ILog public interface ILog { string Save(string message); } 类TxtLog实现接口ILog publ ...

  2. spring 监听器简介

    在java web项目中我们通常会有这样的需求:当项目启动时执行一些初始化操作,例如从数据库加载全局配置文件等,通常情况下我们会用javaee规范中的Listener去实现 常用的监听器有spring ...

  3. Collection of Boot Sector Formats for ISO 9660 Images

    http://bazaar.launchpad.net/~libburnia-team/libisofs/scdbackup/view/head:/doc/boot_sectors.txt Colle ...

  4. Boosting AdaBoosting Algorithm

    http://math.mit.edu/~rothvoss/18.304.3PM/Presentations/1-Eric-Boosting304FinalRpdf.pdf Consider MIT ...

  5. 调用Windows API实现GBK和UTF-8的相互转换

    GBK转UTF-8示例 GbkToUtf8.cpp #include <Windows.h> #include <iostream> #include <string&g ...

  6. Javascript高级程序设计笔记(很重要尤其是对象的设计模式与继承)

    var obj = {'a':'a'}; var fun = function (){} console.log(typeof obj);//object console.log(typeof fun ...

  7. linux CentOS7.2安装ffmpeg-3.0.2

    ffmpeg是一款视频处理软件,在php5.5前以php_ffmpeg.dll扩展的方式存在,通过网上查资料显示,从php5.5以后不支持该扩展了.那么在linux系统下安装ffmpeg.方法如下: ...

  8. 人生苦短之Python文件的IO操作

    在Python中也有涉及到文件的相关操作,从最简单的文件读取说起 文件读取 file = open('/Users/macbookpro/Desktop/使用教程.txt', 'r', encodin ...

  9. 谈谈网站测试中的AB测试方法

    什么是A/B测试? A / B测试,即你设计的页面有两个版本(A和B),A为现行的设计, B是新的设计.比较这两个版本之间你所关心的数据(转化率,业绩,跳出率等) ,最后选择效果最好的版本. A / ...

  10. linux系统配置之服务程序的开机自启动(centos)

    CentOS安装好apache.mysql等服务器程序后,并没有设置成开机自动启动的,为避免重启后还要手动开启web等服务器,还是做下设置好,其实设置很简单,用chkconfig命令就行了. 例如,要 ...