【leetcode】1095. Find in Mountain Array
题目如下:
(This problem is an interactive problem.)
You may recall that an array
A
is a mountain array if and only if:
A.length >= 3
- There exists some
i
with0 < i < A.length - 1
such that:
A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]
Given a mountain array
mountainArr
, return the minimumindex
such thatmountainArr.get(index) == target
. If such anindex
doesn't exist, return-1
.You can't access the mountain array directly. You may only access the array using a
MountainArray
interface:
MountainArray.get(k)
returns the element of the array at indexk
(0-indexed).MountainArray.length()
returns the length of the array.Submissions making more than
100
calls toMountainArray.get
will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.Example 1:
Input: array = [1,2,3,4,5,3,1], target = 3
Output: 2
Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.Example 2:
Input: array = [0,1,2,4,2,1], target = 3
Output: -1
Explanation: 3 does not exist inthe array,
so we return -1.Constraints:
3 <= mountain_arr.length() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9
解题思路:我的解法是二分查找。mountain array 数组的特点是有一个顶点,顶点左边的区间是单调递增,右边的区间是单调递减。所以首先是找出顶点的下标,对于任意一个点mid,如果值比(mid-1)和(mid+1)都大,表示这个是顶点;如果mid的值大于(mid+1),表示mid处于下降区间,令high = mid - 1;如果mid的值大于(mid-1),表示mid处于上升区间,令low = mid + 1;最终可以计算出顶点top。接下来再对左边的上升区间做二分查找求target,如果找到则返回对应小标;没有的话继续对右边的下降区间用二分查找。
代码如下:
# """
# This is MountainArray's API interface.
# You should not implement it, or speculate about its implementation
# """
#class MountainArray(object):
# def get(self, index):
# """
# :type index: int
# :rtype int
# """
#
# def length(self):
# """
# :rtype int
# """ class Solution(object):
def findInMountainArray(self, target, mountain_arr):
"""
:type target: integer
:type mountain_arr: MountainArray
:rtype: integer
"""
length = mountain_arr.length()
low = 0
high = length - 1
while low <= high:
mid = (low + high)/2
mid_val = mountain_arr.get(mid)
mid_l_val,mid_h_val = -float('inf'),-float('inf')
if mid - 1 >= 0:
mid_l_val = mountain_arr.get(mid-1)
if mid + 1 <= high:
mid_h_val = mountain_arr.get(mid+1)
if mid_val > mid_l_val and mid_val > mid_h_val:
break
elif mid_val > mid_h_val:
high = mid - 1
elif mid_val > mid_l_val:
low = mid + 1
#left
low,high = 0,mid
res = -1
while low <= high:
mid = (low + high)/2
mid_val = mountain_arr.get(mid)
if target == mid_val:
res = mid
break
elif target > mid_val:
low = mid + 1
else:
high = mid - 1 if res != -1:return res
low, high = mid,length-1
while low <= high:
mid = (low + high)/2
mid_val = mountain_arr.get(mid)
if target == mid_val:
res = mid
break
elif target < mid_val:
low = mid + 1
else:
high = mid - 1
return res
【leetcode】1095. Find in Mountain Array的更多相关文章
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值
[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】1095. 山脉数组中查找目标值 Find in Mountain Array
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetco ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【LeetCode】697. Degree of an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
随机推荐
- 阶段3 1.Mybatis_04.自定义Mybatis框架基于注解开发_1 今日课程内容介绍
- Apache hadoop namenode ha和yarn ha ---HDFS高可用性
HDFS高可用性Hadoop HDFS 的两大问题:NameNode单点:虽然有StandbyNameNode,但是冷备方案,达不到高可用--阶段性的合并edits和fsimage,以缩短集群启动的时 ...
- MACBOOK蓝牙连接丢失恢复
MACBOOK用户可能会碰到一个神奇而无奈的问题: 某些情况下,很多时候从睡眠状态回来,会发现蓝牙设备再也无法连接,包括蓝牙鼠标.键盘等等... 恢复方法一般是,重启或注销后重新登陆,然后所有工作现场 ...
- C#新特性span 和 Tuple
span 可用于高性能字符串分割等 https://www.cnblogs.com/lonelyxmas/p/10171869.html https://www.codemag.com/article ...
- (转载)gcc编译选项总结
转载自:https://blog.csdn.net/gatieme/article/details/21389603 常用编译选项 gcc and g++分别是gnu的c & c++编译器 g ...
- SELECT 与 SET给标量赋值
本文转自 :https://blog.csdn.net/perddy/article/details/4033406 SQL Server推荐使用 SET 而不是 SELECT 对变量进行赋值.当表达 ...
- 跟风Manacher算法整理
这是上上周天机房一位神仙讲的,\(gu\)了这么久才来整理\(w\),神仙讲的基本思路已经全都忘记了,幸好的是神仙写了\(blog\),吹爆原博浅谈\(Manacher\)算法,以及原博神仙\(ych ...
- 补充[BNDSOJ]小p的数列
强烈安利gjz的题解,看一遍即可ac:传送门 进入重点: 为啥$to=(dp[i][k][ii]+dp[k+1][j][jj])/2$ 位运算重点:a&b=a+b-a|b 为啥呢? 例子: a ...
- 一致性Hash算法(转)
一致性Hash算法提出了在动态变化的Cache环境中,判定哈希算法好坏的四个定义: 1.平衡性(Balance):平衡性是指哈希的结果能够尽可能分布在所有的缓冲(Cache)中去,这样可以使得所有的缓 ...
- 【vuejs面试题】务必熟知的vuejs面试题「务必收藏」
如果能帮到你,点个赞吧,务必熟知的vuejs面试题「务必收藏」 vuejs 基础必备 1.active-class 是哪个组件的属性?嵌套路由怎么定义 (1).active-class 是 vue-r ...