binary search - finding local minima
Anonymous User
3839

given an array of num, find the index of local minima.

[3,2,1,4,5,2,6]
-> answer could be either 2 (where 1 located) or 5 (where 2 located)

I thought I could solve this problem based on solution with (https://leetcode.com/problems/find-peak-element/) but little bit not sure what to tweak.

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

above is my solution for find peak lement.
if I change this to finding local minima, what should I do?
one thing I thought of was just changing if comparison to nums[mid-1] > nums[mid] and nums[mid] < nums[mid+1]

I will appreciate any help.

Comments (3)