|
| 1 | +--- |
| 2 | +id: Find-Peak-Element |
| 3 | +title: Find Peak Element |
| 4 | +sidebar_label: 0162-Find-Peak-Element |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Binary Search |
| 8 | +description: "A peak element is an element that is strictly greater than its neighbors. |
| 9 | +
|
| 10 | +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. |
| 11 | +
|
| 12 | +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. |
| 13 | +
|
| 14 | +You must write an algorithm that runs in O(log n) time." |
| 15 | +--- |
| 16 | + |
| 17 | + |
| 18 | +### Problem Description |
| 19 | + |
| 20 | +A peak element is an element that is strictly greater than its neighbors. |
| 21 | + |
| 22 | +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. |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +You must write an algorithm that runs in O(log n) time. |
| 27 | + |
| 28 | +### Examples |
| 29 | + |
| 30 | +#### Example 1 |
| 31 | + |
| 32 | +- **Input:** `nums = [1,2,3,1]` |
| 33 | +- **Output:** `2` |
| 34 | + |
| 35 | +#### Example 2 |
| 36 | + |
| 37 | +- **Input:** `nums = [1,2,1,3,5,6,4]` |
| 38 | +- **Output:** `5` |
| 39 | + |
| 40 | +### Constraints |
| 41 | + |
| 42 | +- `1 <= nums.length <= 1000` |
| 43 | +- `-231 <= nums[i] <= 231 - 1` |
| 44 | +- `nums[i] != nums[i + 1]` for all valid `i`. |
| 45 | + |
| 46 | +### Solution Code |
| 47 | + |
| 48 | +#### Python |
| 49 | + |
| 50 | +```python |
| 51 | +class Solution: |
| 52 | + def findPeakElement(self, nums: list[int]) -> int: |
| 53 | + left, right = 0, len(nums) - 1 |
| 54 | + |
| 55 | + while left < right: |
| 56 | + mid = (left + right) >> 1 |
| 57 | + |
| 58 | + if nums[mid - 1] <= nums[mid] >= nums[mid + 1]: |
| 59 | + return mid |
| 60 | + elif nums[mid] < nums[mid + 1]: |
| 61 | + left = mid + 1 |
| 62 | + else: |
| 63 | + right = mid |
| 64 | + |
| 65 | + return left |
| 66 | +``` |
| 67 | + |
| 68 | +#### Java |
| 69 | + |
| 70 | +```java |
| 71 | +class Solution { |
| 72 | + public int findPeakElement(int[] nums) { |
| 73 | + int n=nums.length; |
| 74 | + if(n==1)return 0; |
| 75 | + if(nums[0]>nums[1])return 0; |
| 76 | + if(nums[n-1]>nums[n-2])return n-1; |
| 77 | + int low=1,high=n-2; |
| 78 | + while(low<=high){ |
| 79 | + int mid=(low+high)/2; |
| 80 | + if(nums[mid]>nums[mid-1] && nums[mid]>nums[mid+1])return mid; |
| 81 | + else if(nums[mid]>nums[mid-1])low=mid+1; |
| 82 | + else high=mid-1; |
| 83 | + } |
| 84 | + return -1; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +#### C++ |
| 90 | + |
| 91 | +```cpp |
| 92 | + |
| 93 | +class Solution { |
| 94 | +public: |
| 95 | + int findPeakElement(vector<int>& nums) { |
| 96 | + int low = 0, high = nums.size() - 1; |
| 97 | + while (low < high) { |
| 98 | + int mid = low + (high - low) / 2; |
| 99 | + if (nums[mid] < nums[mid + 1]) { |
| 100 | + low = mid + 1; |
| 101 | + } else |
| 102 | + high = mid; |
| 103 | + } |
| 104 | + return low; |
| 105 | + } |
| 106 | +}; |
| 107 | +``` |
| 108 | +#### Javascript |
| 109 | +
|
| 110 | +```javascript |
| 111 | +class Solution { |
| 112 | + public int findPeakElement(int[] nums) { |
| 113 | + int n = nums.length; |
| 114 | + if(n == 1) |
| 115 | + return 0; |
| 116 | + else if(nums[0] > nums[1]) |
| 117 | + return 0; |
| 118 | + else if(nums[n-1] > nums[n-2]) |
| 119 | + return n-1; |
| 120 | + int low = 1, high = n-2; |
| 121 | + while(low <= high) { |
| 122 | + int mid = low + (high - low)/2; |
| 123 | + if(nums[mid] > nums[mid-1] && nums[mid] > nums[mid+1]) |
| 124 | + return mid; |
| 125 | + else if(nums[mid] < nums[mid-1]) |
| 126 | + high = mid-1; |
| 127 | + else |
| 128 | + low = mid+1; |
| 129 | + } |
| 130 | + return -1; |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
0 commit comments