This solution gives a time limit exceeded on leetcode but was really easy to understand. Thank you for explaining
@aminebesbes62022 жыл бұрын
The solution could be more simplified as this (Easy Binary Search with no need to check boundaries): class Solution: def findPeakElement(self, nums: List[int]) -> int: left, mid, right = 0, 0, len(nums)-1 while leftnums[mid+1]: right = mid else: left = mid +1 return left
@rdnetalaАй бұрын
easy recursrive sol: class Solution: def findPeakElement(self, nums: List[int]) -> int: def bs(left, right): if left == right: return left # Base case: single element, so it’s a peak mid = (left + right) // 2 # Compare middle with the next element to decide which side to search if nums[mid] > nums[mid + 1]: # Peak is in the left half return bs(left, mid) else: # Peak is in the right half return bs(mid + 1, right) return bs(0, len(nums) - 1)