I have an array p with integers .
I need to compute d[i, j] for all (i, j) : i<j , where :
d[i, j] = max (p[b] - p[a]) , i<=a<b<=j
I know how to solve this for d[1,n] in O(n) . But for every pair ? Here's my thoughts:
- I can solve it in O(n^3) : Basically I can use the solution for d[1, n] for every subarray : Let's say I want d[i, j]:
right = j
left = i
mx = p[j]
mn = p[i]
while(left < right):
if(p[right] > mx)
mx = p[right]
if(p[left] < mn)
mn = p[left]
right--; left ++;
return (mx-mn)
Now I can repeat for every pair : #pairs = n^2 and therefore O(n^3).
But there must be something faster.
- I can see that if
d[i, j] = p[m] - p[l]thend[a, b] = d[i, j] for all a <= m < l <=b(basically the [l, m] is contained in [a, b]) - I can also see that if d[i, j] = p[m]-p[l] and m < j and l > i then this difference will get bigger iff there exists : p[q] < p[l] or p[m]
- Since , I need to solve n^2 subproblems even if I solve them in O(1), the time complexity has a lower bound to write these results so
T(n) = Ω(n^2)
Can you help ?