LeetCode941. Valid Mountain Array

2023-11-10 05:52

本文主要是介绍LeetCode941. Valid Mountain Array,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、题目
    • 二、题解

一、题目

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

Recall that arr is a mountain array if and only if:

arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < … < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > … > arr[arr.length - 1]

Example 1:

Input: arr = [2,1]
Output: false
Example 2:

Input: arr = [3,5,5]
Output: false
Example 3:

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

Constraints:

1 <= arr.length <= 104
0 <= arr[i] <= 104

二、题解

class Solution {
public:bool validMountainArray(vector<int>& arr) {int n = arr.size();if(n < 3) return false;int maxVal = *max_element(arr.begin(),arr.end());int index = find(arr.begin(),arr.end(),maxVal) - arr.begin();if(index == 0 || index == n - 1) return false;int pre = arr[0];for(int i = 1;i < n;i++){int cur = arr[i];if(i < index && (cur <= pre || cur >= maxVal)) return false;if(i > index && (cur >= pre || cur >= maxVal)) return false;pre = cur;}return true;}
};

这篇关于LeetCode941. Valid Mountain Array的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/380755

相关文章

解决The valid characters are defined in RFC 7230 and RFC 3986

解决方法: 一、更换低版本的Tomcat;(我选的方案) 二、参考:https://blog.csdn.net/qq_32365919/article/details/82055800

【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。 13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20 #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define INF 1 << 30

leetCode#125. Valid Palindrome

Description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

leetcode#32. Longest Valid Parentheses

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", wh

记一次knife4j文档请求异常 SyntaxError: Unexpected token ‘<‘, ... is not valid JSON

knife4j页面报错问题定位 前几天开发新接口,开发完成后想使用knife4j测试一下接口功能,突然发现访问页面报错提示:knife4j文档请求异常,但之前运行还是正常的,想想会不会与升级依赖有关系,启动其他微服务发现文档接口访问正常,排除因依赖版本升级导致在线API文档无法使用情况,还是和本服务新增接口有关系。 定位问题 首先f12打开调试台,重新刷新页面,看到console有报错提示

做一个问卷考试,标准答案对比用户填写的答案,array_diff 进行差集比对

if( empty(array_diff($answer_mark, $answer)) && empty(array_diff( $answer,$answer_mark))){//用户答题正确}else{// 答题错误} 做一个问卷考试,标准答案对比用户填写的答案,array_diff  进行差集比对   如用户填写的答案变量为answer   标准答案为answer_mark

[LeetCode] 238. Product of Array Except Self

题:https://leetcode.com/problems/product-of-array-except-self/description/ 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all

[LeetCode] 215. Kth Largest Element in an Array

题:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not th

MyBatis - 使用foreach迭代List/Array的说明

在 MyBatis 中的 foreach 元素,主要用于迭代 集合数据 以动态生成执行语句;主要有 item、index、collection、open、separator、close 等属性 属性说明         collection:要迭代的数据集对象,必填项         item:迭代出的元素的别名,必填项         index:元素的序号(map时为k