首页 > 其他分享 >LeetCode 852. Peak Index in a Mountain Array 二分

LeetCode 852. Peak Index in a Mountain Array 二分

时间:2023-07-18 22:46:47浏览次数:31  
标签:Index arr 852 ... Mountain mid length left

An array arr a mountain if the following properties hold:

  • 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]

Given a mountain array arr, return the index i such that \(arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1]\).

You must solve it in \(O(\log(arr.length))\) time complexity.

Solution

点击查看代码
class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left = 0
        right = len(arr)-1

        while left<right:
            mid = left+int((right-left)/2)
            if arr[mid+1]>arr[mid]:
                left=mid+1
            else:
                right = mid
        return left

标签:Index,arr,852,...,Mountain,mid,length,left
From: https://www.cnblogs.com/xinyu04/p/17564323.html

相关文章

  • Element-Plus的el-menu-item的index属性问题
    今天用Vue3+Element-Plus开发时,出现了以下问题Invalidprop:typecheckfailedforprop"index".ExpectedString|Null,gotNumberwithvalue8.、上网百度以及结合提示,可以得出结论: <el-menu-item></el-menu-item> 中的index属性,接受的值必须为字符串或null,而我在......
  • MySQL--Sorted Index Builds 导致备份失败故障分析
    问题概述xtrabackup备份失败,日志中有这样的信息InnoDB:Anoptimized(withoutredologging)DDLoperationhasbeenperformed.Allmodifiedpagesmaynothavebeenflushedtothediskyet.问题原因redologs会跳过一些DDL,PerconaXtraBackup监测到redolog有跳过时,它会......
  • jQuery 中 .eq( index ) 的用法
    1、jQuery 中.eq(index) 的用法 :运行下面的例子及明白了。  <!DOCTYPEhtml><html><head><style>div{width:60px;height:60px;margin:10px;float:left;border:2pxsolidblue;}.blue{background:blue;}</style><scr......
  • ORA-20000: Unable to set values for index xxx: does not exist or insufficient pr
    使用expdp/impdp导出导入数据时,遇到ORA-2000错误,如下所示:Processing object type SCHEMA_EXPORT/TABLE/GRANT/OWNER_GRANT/OBJECT_GRANTProcessing object type SCHEMA_EXPORT/TABLE/COMMENTProcessing object type SCHEMA_EXPORT/TABLE/INDEX/INDEXProcessing object......
  • ERROR in static/js/*** from UglifyJs Unexpected token: punc (,) [./~/@vant/poppe
     今天打包的时候发现这样的问题,解决方式:找到webpack.base.conf.js文件,加入下边的代码: {test:/\.mjs$/,loader:"babel-loader",include:[resolve("node_modules/@vant/popperjs/dist/index.esm.mjs")]},......
  • Unable to update index for central http://repo1.maven.org/maven2/
    Unable to update index for central http://repo1.maven.org/maven2/ 就是这句,myeclipse启动后控制台输出这句话:解决办法:1.在myeclipse3.4(我用的这个版本)里面Window => Preferences => Myeclipse Enterprise Workbench => Maven4Myeclipse => Maven=>禁用Downl......
  • java.net.BindException: Address already in use: JVM_Bind <null> 的解决方案
    问题描述在学习SSM整合中,启用Tomcat插件时出现以下错误java.net.BindException:Addressalreadyinuse:JVM_Bind<null>通过查阅资料发现是端口被占用了解决方案通过命令查看进程,这里我的是8080端口号被占用了netstat-ano再运行命令去杀死占用端口进程taskk......
  • 【Python基础】index函数-返回查找对象的首个匹配的索引位置
    描述从列表中找出某个值第一个匹配项的索引位置返回的是查找对象的索引位置,如果没有,就会抛出异常语法List.index(a,start,end)参数解释a要查找的对象(必填)start要查找的范围的开始位置索引(闭区间)(非必填)end要查找的范围的结束位置索引(开区间)(有end就必须有start,有start时可以没end)举......
  • CSS:z-index属性
    如果父div元素设置了z-index:100属性,子div元素的行为将取决于其自身的z-index值和定位方式。子div元素没有显式设置z-index:如果子div元素没有设置position属性,或者设置为static,则子元素的层叠顺序将继承自父元素,并且父元素的z-index值(100)将影响子元素。如......
  • MongoDB索引Index
    MongoDB索引Index索引概述索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对应用的性能是非常致命的创......