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:

  1. Input: [2,1]
  2. Output: false

Example 2:

  1. Input: [3,5,5]
  2. Output: false

Example 3:

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

Note:

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

Approach #1:

  1. class Solution {
  2. public:
  3. bool validMountainArray(vector<int>& A) {
  4. int len = A.size();
  5. if (len < 3) return false;
  6. auto index = max_element(A.begin(), A.end()) - A.begin();
  7. if (index == len - 1 || index == 0) return false;
  8. for (int i = 0; i < index; ++i)
  9. if (A[i] >= A[i+1]) return false;
  10. for (int i = index; i < len-1; ++i)
  11. if (A[i] <= A[i+1]) return false;
  12. return true;
  13. }
  14. };

  

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. 制作一个塔防游戏 Cocos2d-x 2.1.4 (一)

    在这篇文章,将会学习到怎样制作一个塔防游戏.在这其中,学习怎样在设定的时间内出现一波波的敌人,使这些敌人沿着指定的路点前进.怎样在地图上指定的位置创建炮塔.怎样使炮塔射击敌人,怎样可视化调试路点和炮塔 ...

  2. ASP.Net MVC upload file with record & validation - Step 6

    Uploading file with other model data, validate model & file before uploading by using DataAnnota ...

  3. HTML5学习笔记简明版(9):变化的元素和属性

    改变的元素(Element) 下面元素在HTML5里的使用方法稍作改动以便能在web里更好的使用或者起到更大作用: 没有href属性的a元素将显示成一个占位符,并且a元素内部如今支持flow cont ...

  4. Aspose.cells 读取Excel表中的图片问题

    一.说明 本文主要是讲解,怎么使用aspose.cells读取Excel表中的图片,并把图片转换成流或是image对象. 二.开发环境说明 开发工具vs2012,c#语言, 三.Aspose.cell ...

  5. LNK1112: module machine type 'x64' conflicts with target machine type 'X86'

    1 什么是“module machine type” 这个是当前工程要链接的静态库的target machine type. 2 什么是“target machine type” 这个是当前工程生成的 ...

  6. go map 线程不安全 安全措施

    go map   线程不安全  安全措施

  7. Hibernate ManyToOne Mappings 多对一关联映射

    Hibernate ManyToOne Mappings 多对一关联映射 Hibernate框架的使用步骤: 1.创建Hibernate的配置文件(hibernate.cfg.xml)2.创建持久化类 ...

  8. PAT 天梯赛 L3-010. 是否完全二叉搜索树 【Tree】

    题目链接 https://www.patest.cn/contests/gplt/L3-010 思路 因为是 完全二叉搜索树 可以用 数据 建树的方式 然后 遍历一遍这个 数字 就是 层序遍历 遍历的 ...

  9. 【React系列】Props 验证

    Props 验证使用 propTypes,它可以保证我们的应用组件被正确使用,React.PropTypes 提供很多验证器 (validator) 来验证传入数据是否有效.当向 props 传入无效 ...

  10. 关于<context:annotation-config/>配置

    对于spring项目的一些配置,一直感到有些混乱,今天看到一前辈总结的特别好,把自己的理解贴在这里,有不当的地方,后续继续学习: 当我们使用@Autowired.@Required等这些注解时,就要在 ...