[LeetCode] Peak Index in a Mountain Array 山形数组的顶峰坐标
Let's call an array `A` a *mountain* if the following properties hold:
A.length >= 3- There exists some
0 < i < A.length - 1such thatA[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
Example 1:
Input: [0,1,0]
Output: 1
Example 2:
Input: [0,2,1,0]
Output: 1
Note:
3 <= A.length <= 100000 <= A[i] <= 10^6- A is a mountain, as defined above.
这道题定义了一种山形的数组,说是有一个最高点,然后向两边各自降低,让我们找出山峰的位置。其实这道题跟之前那道 [Find Peak Element](http://www.cnblogs.com/grandyang/p/4217175.html) 非常的类似,只不过那道题有很多局部峰值,而这里道题只有一个全局峰值。题目中限定了山峰一定存在,即一定有一个最高点,反应在数组中就是最大值,那么问题就转换为了求数组中最大值的位置,最简单直接的方法就是遍历数组找出最大值的位置即可,这里使用了 STL 的内置函数 max_element() 来一行解题,参见代码如下:
解法一:
class Solution {
public:
int peakIndexInMountainArray(vector<int>& A) {
return max_element(A.begin(), A.end()) - A.begin();
}
};
由于题目中限定了山峰一定存在,所以我们也可以直接直接来找第一个下降的位置,即 A[i] > A[i+1] 的地方,那么i位置一定就是山峰了,注意i的遍历范围要去掉首尾两个数字,参见代码如下:
解法二:
class Solution {
public:
int peakIndexInMountainArray(vector<int>& A) {
for (int i = 1; i < (int)A.size() - 1; ++i) {
if (A[i] > A[i + 1]) return i;
}
return 0;
}
};
上面两种解法都是线性的时间复杂度,能不能更快一点呢?那么就只有使用二分搜索法来加快搜索速度了,其实这是博主的总结帖 [LeetCode Binary Search Summary 二分搜索法小结](http://www.cnblogs.com/grandyang/p/6854825.html) 中的第五类情况,跟前的稍有不同的是 right 的初始化,之前的情况博主基本上都是用数组长度初始化 right 的,但是这里要用数组长度减1来初始化 right,因为要跟紧邻的右边的数字比较,这样初始化 right 的意义在于 mid+1 就不会越界了,参见代码如下:
解法三:
class Solution {
public:
int peakIndexInMountainArray(vector<int>& A) {
int n = A.size(), left = 0, right = n - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (A[mid] < A[mid + 1]) left = mid + 1;
else right = mid;
}
return right;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/852
类似题目:
参考资料:
https://leetcode.com/problems/peak-index-in-a-mountain-array/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Peak Index in a Mountain Array 山形数组的顶峰坐标的更多相关文章
- LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)
题目标签:Binary Search 题目给了我们一组 int array,让我们找到数组的 peak. 利用 binary search, 如果数字比它后面那个数字小,说明还在上坡,缩小范围到右半边 ...
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array)
Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array) 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 ...
- 【Leetcode_easy】852. Peak Index in a Mountain Array
problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...
- 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...
- C#LeetCode刷题之#852-山脉数组的峰顶索引(Peak Index in a Mountain Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4003 访问. 我们把符合下列属性的数组 A 称作山脉: A.le ...
- LeetCode 852 Peak Index in a Mountain Array 解题报告
题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...
- LeetCode算法题-Peak Index in a Mountain Array(Java实现)
这是悦乐书的第329次更新,第352篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第199题(顺位题号是852).如果以下属性成立,我们将数组A称为山: A.length ...
- LeetCode 852. Peak Index in a Mountain Array(C++)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
随机推荐
- 【转载】Centos7修改root密码
参考: https://blog.csdn.net/wcy00q/article/details/70570043 知道root密码,需要修改密码 以root登录系统输入passwd命令默认修改roo ...
- 补记:完成了NG的SP1的全部内容 开始第二周
DL本质上就是多层的Logistics Regression with different activation function and nicely designed back propagati ...
- RNN回归
import torch from torch import nn import numpy as np import matplotlib.pyplot as plt # torch.manual_ ...
- Python——类的封装
class Gun: def __init__(self, model): # 1. 枪的型号 self.model = model # 2. 子弹的数量 self.bullet_count = 0 ...
- WPF 10天修炼 第二天- XAML语言
XAML是什么 XAML是一种与.NET CLR紧密集成的声明性UI标记语言.XAML中的对象元素对应到CLR中的类型或结构.XAML命名空间对应到CLR中类的命名空间,元素类型则对应到CLR中的类型 ...
- 使用gethostname()函数和gethostbyname()函数获取主机相关信息
gethostname() : 返回本地主机的标准主机名. 原型如下: #include <unistd.h> int gethostname(char *name, size_t len ...
- 初识C语言 (四)
分支结构 if语句 C语言中的分支结构语句中的if条件语句,简单if语句的基本结构如下: 其语义是:如果表达式的值为真,则执行其后的语句,否则不执行该语句. 其过程可表示为下图 实例: if(resu ...
- vue.js遍历ajax请求的数据
<div id="dv" style="text-align: center;"><div class="head input-gr ...
- string对象方法
一:str.isalnum() ,str.isalpha(),str.isdigit() ,str.islower() ,str.isupper() 1.str.isalnum() This meth ...
- 使用X509Certificate2类操作证书文件
public class CertHelper { string pfxPath = @"E:\开发辅助项目\cert\taisuyuanqing.pfx"; string cer ...