---
# System prepended metadata

title: 162. Find Peak Element
tags: [leetcode, '162', medium]

---


# 162. Find Peak Element
###### tags: `leetcode` `162` `medium` 

## :memo:  Question
![](https://i.imgur.com/8a2igIN.png)

## :memo: 題意
* 給你一個 list，找出 peak 點，peak 指的是 比左邊和右邊的值都大。然後要符合 O(logn)

## :memo: leetcode solution
* :medal: **思考一**: list 要用 logn 那可能就要往 binary search 的地方想
* :medal: **思考二**: 判斷要往右半邊找還是左半邊找的依據是 nums[m]+1 > nums[m] => l = m + 1
* else r = m



## :memo: my solution code
```python=
class Solution:
    def findPeakElement(self, nums: List[int]) -> int:
        if not nums:
            return -1

        low, high = 0, len(nums) - 1
        
        while low <= high:
            if low == high:
                return low
            mid = (low + high) // 2
            if nums[mid] < nums[mid+1]:
                low = mid + 1
            else:
                high = mid

        return mid
    
```

## :memo: BigO
* 時間複雜度: O(log n)。
* 空間複雜度: O(1)。 