Created
September 18, 2019 15:43
-
-
Save priyankvex/e09c55155a087ccbf46a8650ee2fbab8 to your computer and use it in GitHub Desktop.
Find the peak element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def findPeakElement(self, a): | |
low = 0 | |
high = len(a) - 1 | |
ans = self.helper(a, low, high) | |
return ans | |
def helper(self, a, low, high): | |
if low >= high: | |
return low | |
mid = int((low + high) / 2) | |
if a[mid] < a[mid+1]: | |
return self.helper(a, mid+1, high) | |
if a[mid] >= a[mid+1]: | |
return self.helper(a, low, mid) | |
if __name__ == "__main__": | |
a = [2, 1, 3] | |
ans = Solution().solve(a) | |
print(ans) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment