Find Peak Element

Find Peak Element

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

Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.

You may imagine that nums[-1] = nums[n] = -∞. In other words, an element is always considered to be strictly greater than a neighbor that is outside the array.

You must write an algorithm that runs in O(log n) time.

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: 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.

?

Constraints:

  • 1 <= nums.length <= 1000
  • -231 <= nums[i] <= 231 - 1
  • nums[i] != nums[i + 1] for all valid i.


SOLUTION:




要查看或添加评论,请登录

Bekir Sahin的更多文章

  • Mastering Prompt Engineering: A Comprehensive Framework

    Mastering Prompt Engineering: A Comprehensive Framework

    In the rapidly evolving fields of artificial intelligence and data science, clear and precise instructions are vital…

    1 条评论
  • Simplify Path

    Simplify Path

    Given an absolute path for a Unix-style file system, which begins with a slash '/', transform this path into its…

  • Combinations

    Combinations

    Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n]. You may return…

  • Add Binary

    Add Binary

    Given two binary strings a and b, return their sum as a binary string. Example 1: Input: a = 11, b = 1 Output: 100…

  • Summary Ranges

    Summary Ranges

    You are given a sorted unique integer array nums. A range [a,b] is the set of all integers from a to b (inclusive).

  • Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters

    Given a string s, find the length of the longest substring without repeating characters. Example 1: Input: s =…

  • Integer to Roman

    Integer to Roman

    Seven different symbols represent Roman numerals with the following values: Symbol Value I 1 V 5 X 10 L 50 C 100 D 500…

  • Minimum Size Subarray Sum

    Minimum Size Subarray Sum

    Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose…

  • Reverse Words in a String

    Reverse Words in a String

    Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters.

  • Rotate Array

    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…

社区洞察