Given a list that can be broken into two pieces L[:z] and L[z:] such that the first is non-increasing and the second is non-decreasing and may or may not contain duplicate elements, create a function such that:
Input:
- A list L as specified above
- A value k which represents the maximum number of times any given value is repeated in the list
Output:
- The minimum value of the list
Complexity & Requirements
- O(log n)
- Can only use built in library functions not including min/max
I have the following:
def findmin(L, k = None):
left = 0
right = len(L)-1
foundmin = False
while not foundmin:
mp = (left + right)//2
if L[mp] > L[mp+1]:
left = mp
elif L[mp-1] < L[mp]:
right = mp+1
else:
return L[mp]
It only works for some lists such as: L = [9,9,4,3,2,1,7,7,10,10]
But it does not work correctly for lists such as: L = [6,5,4,3,3,3,3,3,3,3,3,3,1,7,7,7,7]
I have tried modifying the function to accommodate repeated elements to no avail. I am also not seeing the utility of the input k for the function. Thoughts?