Now that I have N different intergers, I need to find the interval that has the largest number whose value is between the endpoints of the O (NlogN) time interval. I call this the divide and conquer problem because it is in my final divide and win exam. I thought about this for 2 weeks and did a lot of experiments, none of them are correct (compared to brute force algorithm). Can someone help me?
examples:
8,1,3,4,7. The answer is 1-7.
2,6,5,4,9,8. The answer is 2-9 or 2-8.
I think the word "interval" does not express my meaning. I want to find the subsequence of the array with the largest number whose value is between the endpoints of the subsequence. For example: “1,3,4,7” has two numbers (3,4) and, for example, 2: both “2,6,5,4,9” and “2,6,5,4,9, 8 "have three numbers (6,5,4).
here is my code (O (n ^ 2)). @Vaughn Cato I use this to compare with your code.
import itertools
def n2(numbers):
a = [0]*len(numbers)
ans = -1
l = 0
r = 0
for j in range(1,len(numbers)):
t = 0
for i in range(j-1,-1,-1):
if numbers[i]<numbers[j]:
x = t - a[i]
if x>ans:
ans = x
l = i
r = j
t += 1
else:
a[i] += 1
return (numbers[l],numbers[r],ans)
def countBetween(numbers,left,right):
cnt = 0
for i in range(left+1,right):
if numbers[left]<numbers[i]<numbers[right]:
cnt += 1
return cnt
for numbers in itertools.permutations(range(5)):
ans1=n2(numbers)
ans2=longestInterval(numbers)
if(ans1[2]!=ans2[2]):
print ans1,ans2,numbers