C#LeetCode刷题之#852-山脉数组的峰顶索引(Peak Index in a Mountain Array)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4003 访问。
我们把符合下列属性的数组 A 称作山脉:
- A.length >= 3
- 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 的 i 的值。
输入:[0,1,0]
输出:1
输入:[0,2,1,0]
输出:1
提示:
- 3 <= A.length <= 10000
- 0 <= A[i] <= 10^6
- A 是如上定义的山脉
Let's call an array A a mountain if the following properties hold:
- A.length >= 3
- There exists some 0 < i < A.length - 1 such that A[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].
Input: [0,1,0]
Output: 1
Input: [0,2,1,0]
Output: 1
Note:
- 3 <= A.length <= 10000
- 0 <= A[i] <= 10^6
- A is a mountain, as defined above.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4003 访问。
public class Program {
public static void Main(string[] args) {
var A = new int[] { 24, 69, 100, 99, 79, 78, 67, 36, 26, 19 };
var res = PeakIndexInMountainArray(A);
Console.WriteLine(res);
Console.ReadKey();
}
private static int PeakIndexInMountainArray(int[] A) {
//二分法
var low = 1;
var high = A.Length - 2;
var mid = 0;
while(low <= high) {
mid = low + (high - low) / 2;
var top = Position(A, mid);
if(top == 1) {
high = mid - 1;
} else if(top == -1) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
}
private static int Position(int[] A, int index) {
//-1,index在峰顶左边
//1,index在峰顶右边
//0,index是峰顶
if(A[index] > A[index - 1] && A[index + 1] > A[index]) return -1;
if(A[index + 1] < A[index] && A[index] < A[index - 1]) return 1;
return 0;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4003 访问。
2
分析:
显而易见,以上算法的时间复杂度为: 。
C#LeetCode刷题之#852-山脉数组的峰顶索引(Peak Index in a Mountain Array)的更多相关文章
- [Swift]LeetCode852. 山脉数组的峰顶索引 | Peak Index in a Mountain Array
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array)
Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array) 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 ...
- 力扣(LeetCode) 852. 山脉数组的峰顶索引
我们把符合下列属性的数组 A 称作山脉: A.length >= 3 存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] ...
- LeetCode 852. 山脉数组的峰顶索引 (二分)
题目链接:https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/ 我们把符合下列属性的数组 A 称作山脉: A.length ...
- C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- 【Leetcode_easy】852. Peak Index in a Mountain Array
problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...
- LeetCode刷题--26.删除排序数组中的重复项(简单)
题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(1)额外空间的条件下完成. 示例 ...
- C#LeetCode刷题之#189-旋转数组(Rotate Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3700 访问. 给定一个数组,将数组中的元素向右移动 k 个位置, ...
随机推荐
- 软件测试工程师应该怎样规划自己?成为年薪30W+测试工程师(乾坤未定,皆是黑马)
今天在知乎上被邀了一个问题,软件测试工程师应该怎样规划自己?16年毕业,技术方面已经渣到不行,因为之前的公司没有Python自动化测试这个要求,有些迷茫.我把我的问题回答贴出来希望可以帮助到更多有类型 ...
- OSCP Learning Notes - Exploit(1)
Gaining Root with Metasploit Platform: Kali Linux, Kioptrix Level 1 1. Find the IP of Kioptirx nmap ...
- Ethical Hacking - NETWORK PENETRATION TESTING(23)
Detecting ARP Posionning Attacks ARP main security issues: 1. Each ARP requests/response is trusted. ...
- 图论相关知识(DFS、BFS、拓扑排序、最小代价生成树、最短路径)
图的存储 假设是n点m边的图: 邻接矩阵:很简单,但是遍历图的时间复杂度和空间复杂度都为n^2,不适合数据量大的情况 邻接表:略微复杂一丢丢,空间复杂度n+m,遍历图的时间复杂度为m,适用情况更广 前 ...
- django-celery 版本 常用命令
http://celery.github.io/django-celery/introduction.html #先启动服务器 python manage.py runserver #再启动worke ...
- 源码分析清楚 AbstractQueuedSynchronizer
本文关注以下几点内容: 深入理解 ReentrantLock 公平锁和非公平锁的区别 深入分析 AbstractQueuedSynchronizer 中的 ConditionObject 深入理解 j ...
- Postman安装失败
https://blog.csdn.net/qq_21282443/article/details/86213972
- jmeter接口测试 -- 设置跨线程组的全局变量
一.操作步骤 1.先提取被设置的变量 2.再用 [线程组] - [后置处理] - [BeanShell PostProcessor]来设置跨线程的全局变量:${__setProperty(新变量名,$ ...
- 火车进栈(进出栈的模拟,dfs爆搜)
这里有n列火车将要进站再出站,但是,每列火车只有1节,那就是车头. 这n列火车按1到n的顺序从东方左转进站,这个车站是南北方向的,它虽然无限长,只可惜是一个死胡同,而且站台只有一条股道,火车只能倒着从 ...
- 巩固复习(Django最基础的部分_具体查看官方文档)
Django学习路1 1.脚本不能随便运行,没准 linux 运行完就上不去了 2.pip 在 linux 上 写 pip3 同理 python 写为 python3 3.在 pycharm 上安装库 ...