LeetCode 162. Find Peak Element

简介: 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。

v2-647dc26cdca0c694dc3777f1cdcfb09d_1440w.jpg

Description



A peak element is an element that is greater than its neighbors.


Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.


The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.


You may imagine that nums[-1] = nums[n] = -∞.


Example 1:

Input: nums = [1,2,3,1]

Output: 2

Explanation: 3 is a peak element and your function should return the index number 2.


Example 2:

Input: nums = [1,2,1,3,5,6,4]

Output: 1 or 5


Explanation: Your function can return either index number 1 where the peak element is 2,

or index number 5 where the peak element is 6.


Note:

Your solution should be in logarithmic complexity.


描述



峰值元素是指其值大于左右相邻值的元素。


给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。

数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。


你可以假设 nums[-1] = nums[n] = -∞。


示例 1:

输入: nums = [1,2,3,1]

输出: 2

解释: 3 是峰值元素,你的函数应该返回其索引 2。


示例 2:

输入: nums = [1,2,1,3,5,6,4]

输出: 1 或 5

解释: 你的函数可以返回索引 1,其峰值元素为 2;

或者返回索引 5, 其峰值元素为 6。


说明:

你的解法应该是 O(logN) 时间复杂度的。


思路



  • 此题目使用二分查找.
  • 如果当前值小于右边的值,说明峰值应该在右边(不包括当前节点),如果当前值大于右边的值,说明峰值应该当前值左边(包括当前节点).
  • 找到的条件是到达了边界或者当前值比左右两边的值都大.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-01-16 10:40:41
# @Last Modified by:   何睿
# @Last Modified time: 2019-01-16 11:21:29
class Solution(object):
    def findPeakElement(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        count = len(nums)
        middle, left, right = 0, 0, count - 1
        while left <= right:
            # 取中间值
            middle = left + ((right - left) >> 1)
            # 如果当前是递增的序列,说明较大值在右边(不包括当前节点)
            if middle < count - 1 and nums[middle] < nums[middle + 1]:
                left = middle + 1
            # 如果当前是递减的徐磊,说明较大值在左边(包括当前节点)
            if middle < count - 1 and nums[middle] > nums[middle + 1]:
                right = middle
            # 结束条件:到达了边界或者当前值比左右两个值大
            if (middle==0 or nums[middle] > nums[middle - 1]) and (middle == count-1 or nums[middle] > nums[middle + 1]):
                return middle
        return middle


源代码文件在这里.


目录
相关文章
|
6月前
|
Java
Leetcode 295. Find Median from Data Stream
在一个有序数组中找中位数,但需要支持再数组中添加新的元素。本来是有序里的,可以很轻易就查到中位数,但如果添加新数字后,不一定有序。如果先对数组排序,那代价就比较大了,每次排序时间复杂度O(n*log(n)),看discuss发现了一种很巧妙的解法,可以把添加数据的时间复杂度降低到O(log(n)) ,查询中位数O(1)。
25 0
|
6月前
Leetcode 230. Kth Smallest Element in a BST
先来看下二叉搜索树的性质,对于任意一个非叶子节点,它的左子树中所有的值必定小于这个节点的val,右子树中所有的值必定大于或等于当前节点的val。 这条性质就保证了如果我们对二叉搜索树做中序遍历,中序遍历的结果肯定是有序的。对于此题而言,我们只需要拿到中序遍历结果,然后返回第k个即可,时间复杂度是O(n)。
46 1
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
LeetCode contest 200 5476. 找出数组游戏的赢家 Find the Winner of an Array Game
|
算法 Python
LeetCode 169. 多数元素 Majority Element
LeetCode 169. 多数元素 Majority Element
|
JavaScript 索引
LeetCode 436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
58 0
LeetCode 436. Find Right Interval
LeetCode 389. Find the Difference
给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
85 0
LeetCode 389. Find the Difference
|
Python
LeetCode 378. Kth S Element in a Sorted Matrix
给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素。 请注意,它是排序后的第k小元素,而不是第k个元素。
77 0
LeetCode 378. Kth S Element in a Sorted Matrix
|
索引
LeetCode 373. Find K Pairs with Smallest Sums
给定两个以升序排列的整形数组 nums1 和 nums2, 以及一个整数 k。 定义一对值 (u,v),其中第一个元素来自 nums1,第二个元素来自 nums2。 找到和最小的 k 对数字 (u1,v1), (u2,v2) ... (uk,vk)。
114 0
LeetCode 373. Find K Pairs with Smallest Sums
|
算法 Python
LeetCode 295. Find Median from Data Stream
中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。
63 0
LeetCode 295. Find Median from Data Stream
|
算法
LeetCode 229. Majority Element II
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
60 0
LeetCode 229. Majority Element II

热门文章

最新文章