Consider an array A = [5,1,7,2,3]
All contiguos subarrays = { [5], [1], [7], [2], [3], [5,1], [1,7], [7,2], [2,3], [5,1,7], [1,7,2], [7,2,3], [5,1,7,2], [1,7,2,3], [5,1,7,2,3] }
Replace all the arrays in the above set with maximum element in it:
set will look like this: { [5], [1], [7], [2], [3], [5], [7], [7], [3], [7], [7], [7], [7], [7], [7] }
Frequency info: [5] -> 2, [1] -> 1, [7] -> 9, [2] -> 1, [3] -> 2
My Goal is to find the above frequency info.
My Approach:
First make a list of pair (x,y). x is element in A and its index is y.
LIST : [ (5,1), (1,2), (7,3), (2,4), (3,5) ]
Sort the list in decreasing order with respect to first element.Now,
LIST : [ (7,3), (5,1), (3,5), (2,4), (1,2) ]
Algorithm:
def f( array, first_index, last_index):
->select an element from LIST starting from left which
is not marked as visited and (first_index <= element.second <=
last_index)
->calculate frequency info of element in tuple as (element.secondvalue-first_index+1) + (element.secondvalue-first_index+1)*(last_index - element.second_value)
->Mark above element as visited
->Recursively solve f( array, first_index,element.secondvalue-1 ),f( array,element.secondvalue+1,last_index)
We can easily set suitable base case.
Time complexity:O(n*n)
I have tried a lot to come with the above algorithm but unable to improve time complexity.How I can do so?Any hint,approach will be appreciated.
I am having hard time trying to explain my solution in words. I will just add the code. It will explain itself:
Explanation for (before[i]+1)*(after[i]+1):
for each value we need the numbers lies before and less than the value and the numbers lies after and less than the value.
Example: for a number that have 3 values less than it and appears before and have 4 values less than it and appears after. answer is V(3,4) = 20 = (3+1) * (4+1)
please, let me know the results.
I doubt your code runs in
O(n^2)
. Anyways, one way to solve this in a more efficient way would be to map each number to the number of items to the left/right which are smaller than the given item. For example:This map will require
O(n^2)
to generate. The rest is straightforward. Given the space to the left and to the right, we can easily determine the number of subarrays that only contains smaller numbers thann
, except forn
itself.Traverse your value-to-index map from bottom to top - maintain an augmented tree of intervals. Each time an index is added, adjust the appropriate interval and calculate the total from the relevant segment:
Insertion and deletion in augmented trees are of
O(log n)
time-complexity. Worst-case total time-complexity isO(n log n)
.Based on Paul's answer, I implemented a O(n log n) version using a simplified version of a segment tree. You'll notice that this answer is the same as Paul's, but with optimized versions of
leftctsmaller
andrightctsmaller
.In practice, what it does is to take an array, let's say:
And construct an array-backed tree that looks like this:
In the tree, the first number is the value and the second is the index where it appears in the array. Each node is the maximum of its two children. This tree is backed by an array (pretty much like a heap tree) where the children of the index
i
are in the indexesi*2+1
andi*2+2
.Then, for each element, it becomes easy to find the nearest greater elements (before and after it).
For example, to find the nearest greater element to the left, we go up in the tree searching for the first parent where the value is greater and the index is less than the argument. The answer must be a child of this parent, then we go down in the tree looking for the rightmost node that satisfies the same condition.
I've implemented it in Python (as well as the naïve version, to check the answer), and it seems to work well.