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 个位置, ...
随机推荐
- js自定义方法绑定元素事件
//事件绑定封装 function addEvent(elem, type, handle){ if(elem.addEventListener){ elem.addEventListener(typ ...
- 第六章:Android的Drawable
Drawable表示的是一种可以在Canvas上进行绘制的抽象的概念. 6.1 Drawable简介 Drawable常被用来作为View的背景使用. Drawable一般都是通过XML来定义的. D ...
- OSCP Learning Notes - Netcat
Introduction to Netcat Connecting va Listening Bind Shells Attacker connects to victim on listening ...
- Ethical Hacking - NETWORK PENETRATION TESTING(9)
WEP Cracking Packet Injection What if the AP was idle, or had no clients associated with it? In this ...
- Python中ftplib模块的使用
ftplib模块的主要接口 # from ftplib import FTP #加载ftp模块 # ftp=FTP() #设置变量 # ftp.set_debuglevel(2) #打开调试级别2,显 ...
- [日常摘要] -- ThreadLocal篇
简介 ThreadLocal,即线程变量,是一个以ThreadLocal对象为键.任意对象为值的存储结构.这个结构被附带在线程上,也就是说一个线程可以根据一个ThreadLocal对象查询到绑定在这个 ...
- Python新手学习raise用法
当程序出现错误时,系统会自动引发异常.除此之外,Python也允许程序自行引发异常,自行引发异常使用 raise 语句来完成. 很多时候,系统是否要引发异常,可能需要根据应用的业务需求来决定,如果程序 ...
- MongoDB 事务,复制和分片的关系
摘要:本文尝试对Mongo的复制和分布式事务的原理进行描述,在必要的地方,对实现的正确性进行论证,希望能为MongoDB内核爱好者提供一些参考. 1.前言 MongoDB基于wiredTiger提供的 ...
- spring tx——TransactionManger
TransactionDefinition--事务定义 定义事务属性,包括传播级别.隔离级别.名称.超时.只读等 TransactionStatus--事务状态 事务状态,包含事务对象(jdbc为Da ...
- 宽度优先搜索--------迷宫的最短路径问题(dfs)
宽度优先搜索运用了队列(queue)在unility头文件中 源代码 #include<iostream>#include<cstdio>#include<queue&g ...