162. Find Peak Element Medium

@problem@discussion
#Array#Binary Search



1/**
2 * [162] Find Peak Element
3 *
4 * A peak element is an element that is strictly greater than its neighbors.
5 * 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.
6 * 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.
7 * You must write an algorithm that runs in O(log n) time.
8 *  
9 * Example 1:
10 * 
11 * Input: nums = [1,2,3,1]
12 * Output: 2
13 * Explanation: 3 is a peak element and your function should return the index number 2.
14 * Example 2:
15 * 
16 * Input: nums = [1,2,1,3,5,6,4]
17 * Output: 5
18 * 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.
19 *  
20 * Constraints:
21 * 
22 * 	1 <= nums.length <= 1000
23 * 	-2^31 <= nums[i] <= 2^31 - 1
24 * 	nums[i] != nums[i + 1] for all valid i.
25 * 
26 */
27pub struct Solution {}
28
29// problem: https://leetcode.com/problems/find-peak-element/
30// discuss: https://leetcode.com/problems/find-peak-element/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34impl Solution {
35    pub fn find_peak_element(nums: Vec<i32>) -> i32 {
36        0
37    }
38}
39
40// submission codes end
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn test_162() {
48    }
49}
50


Back
© 2025 bowen.ge All Rights Reserved.