leetcode - 852. Peak Index in a Mountain Array

这篇具有很好参考价值的文章主要介绍了leetcode - 852. Peak Index in a Mountain Array。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Description

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.

Example 1:

Input: arr = [0,1,0]
Output: 1

Example 2:

Input: arr = [0,2,1,0]
Output: 1

Example 3:

Input: arr = [0,10,5,2]
Output: 1

Constraints:

3 <= arr.length <= 10^5
0 <= arr[i] <= 10^6
arr is guaranteed to be a mountain array.

Solution

Use binary search to solve this problem. For any middle index, it either locates at the left of the peak, or the right of the peak. If at the left, then discard the left half, otherwise discard the right half.

Time complexity: o ( log ⁡ n ) o(\log n) o(logn)
Space complexity: o ( 1 ) o(1) o(1)文章来源地址https://www.toymoban.com/news/detail-607272.html

Code

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left, right = 0, len(arr) - 1
        while left < right:
            mid = (left + right) >> 1
            if arr[mid - 1] < arr[mid] and arr[mid + 1] < arr[mid]:
                return mid
            elif arr[mid - 1] < arr[mid]:
                left = mid + 1
            elif arr[mid + 1] < arr[mid]:
                right = mid

到了这里,关于leetcode - 852. Peak Index in a Mountain Array的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月11日
    浏览(40)
  • leetCode 162 !Find peak Element

    参考资料:左程云算法课 思路:二分法 首先判断左右两端点是否是峰值, 如果是,则返回其下标,并结束;如果不是,说明左右两端点都小,峰值必然在余下(中间部分)取到,则用二分法检查nums[1…n-2]。 具体地,找中间位置的数,看他是不是峰值,如果不是,那么可以

    2024年02月07日
    浏览(17)
  • 【算法|二分查找No.4】leetcode 852. 山脉数组的峰顶索引

    个人主页:兜里有颗棉花糖 欢迎 点赞👍 收藏✨ 留言✉ 加关注💓本文由 兜里有颗棉花糖 原创 收录于专栏【手撕算法系列专栏】【LeetCode】 🍔本专栏旨在提高自己算法能力的同时,记录一下自己的学习过程,希望对大家有所帮助 🍓希望我们一起努力、成长,共同进步。

    2024年02月05日
    浏览(39)
  • LeetCode //189. Rotate Array

    Given an integer array nums , rotate the array to the right by k steps, where k is non-negative.   Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: nums = [-1,-100,3,99],

    2024年02月13日
    浏览(30)
  • LeetCode //88. Merge Sorted Array

    You are given two integer arrays nums1 and nums2 , sorted in non-decreasing order, and two integers m and n , representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1

    2024年02月12日
    浏览(38)
  • LeetCode --- 1929. Concatenation of Array 解题报告

    Given an integer array  nums  of length  n , you want to create an array  ans  of length  2n  where  ans[i] == nums[i]  and  ans[i + n] == nums[i]  for  0 = i n  ( 0-indexed ). Specifically,  ans  is the  concatenation  of two  nums  arrays. Return  the array  ans . Example 1: Example 2:

    2024年02月09日
    浏览(37)
  • Leetcode array 704 27 189 121 380 238 134 13

    二分搜索: 判断条件是left 和 right 这里就要注意区间开闭的问题 因为需要运行时间时O(1),所以要加上map 删除的时候要把需要删除的变到最后,再直接pop掉 rand用法: 1.rand() 2. nums[rand() % nums.size()]; 3. left+rand() % (right-left)

    2024年02月09日
    浏览(25)
  • leetcode - 2149. Rearrange Array Elements by Sign

    You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers. You should rearrange the elements of nums such that the modified array follows the given conditions: Every consecutive pair of integers have opposite signs. For all integers with the same sign, the order in which they were present in n

    2024年02月20日
    浏览(28)
  • python运行报错 KeyError: “[‘...’] not in index”

    我在使用python指定列读取xlsx数据时遇到这个报错,具体原因不知。 这个错误通常表示你正在尝试访问一个不存在的索引或列。为了解决这个错误,你应该检查正在使用的代码并确定是否存在以下情况之一: 索引或列名错误: 检查是否在 DataFrame 中具有正确的索引或列名,可以

    2024年02月11日
    浏览(32)
  • mybatis-plus中的in的使用,是传Array?还是传List?别再纠结了

    先看个技术题吧。 下面两段代码,执行testFoo,结果分别是什么?   一眼看出来结果的同学,恭喜你,本文内容可以略过。   下面是正文。   我们在查询或更新数据的时候,有时要用到in来过滤数据。比如 SELECT * FROM emax_scbg_order WHERE order_no IN (1305679009380433922,1305405259472830465

    2024年02月14日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包