Returns an array A and finds A pair (I, j) so that A [I] & lt; = A [j] (I & lt; j) and j-I

Source: Internet
Author: User

Question: Give an array A and find A pair (I, j) so that A [I] <= A [j] (I <= j) and j-I is the largest, if there are multiple such location pairs, return the smallest pair of I.

The most direct idea is to find the first position j greater than or equal to A [I] from the end of the array, and the time complexity O (n ^ 2 ).


[Cpp] pair <int, int> find (const vector <int> &)
{
Int n = A. size ();
If (n = 0)
Throw new invalid_argument ("Array's size can't be 0! ");
 
Int target_ I = 0, target_j = 0;
Int max_len = 0;
For (int I = 0; I <n; ++ I)
{
Int j;
For (j = n-1; j> = I; -- j)
If (A [j]> = A [I])
Break;
If (j-I + 1> max_len)
{
Target_ I = I;
Target_j = j;
Max_len = j-I + 1;
}
}
 
Return make_pair <int, int> (target_ I, target_j );
}

Pair <int, int> find (const vector <int> &)
{
Int n = A. size ();
If (n = 0)
Throw new invalid_argument ("Array's size can't be 0! ");

Int target_ I = 0, target_j = 0;
Int max_len = 0;
For (int I = 0; I <n; ++ I)
{
Int j;
For (j = n-1; j> = I; -- j)
If (A [j]> = A [I])
Break;
If (j-I + 1> max_len)
{
Target_ I = I;
Target_j = j;
Max_len = j-I + 1;
}
}

Return make_pair <int, int> (target_ I, target_j );
}
We have slightly optimized the above algorithms. When I = 0, we assume that the rightmost position greater than A [I] Is j0. for I = 1, we do not need to consider the position smaller than j0, because their Interval Length is smaller than j0 + 1, it is impossible to become the optimal solution.


[Cpp] pair <int, int> find (const vector <int> &)
{
Int n = A. size ();
If (n = 0)
Throw new invalid_argument ("Array's size can't be 0! ");
 
Int target_ I = 0, target_j = 0;
Int max_len = 0;
For (int I = 0; I <n; ++ I)
{
Int j;
For (j = n-1; j> target_j; -- j) // check target_j
If (A [j]> = A [I])
Break;
If (j-I + 1> max_len)
{
Target_ I = I;
Target_j = j;
Max_len = j-I + 1;
}
}
 
Return make_pair <int, int> (target_ I, target_j );
}

Pair <int, int> find (const vector <int> &)
{
Int n = A. size ();
If (n = 0)
Throw new invalid_argument ("Array's size can't be 0! ");

Int target_ I = 0, target_j = 0;
Int max_len = 0;
For (int I = 0; I <n; ++ I)
{
Int j;
For (j = n-1; j> target_j; -- j) // check target_j
If (A [j]> = A [I])
Break;
If (j-I + 1> max_len)
{
Target_ I = I;
Target_j = j;
Max_len = j-I + 1;
}
}

Return make_pair <int, int> (target_ I, target_j );
}
However, the time complexity is still O (n ^ 2. We can continue to optimize the above ideas. In fact, for location I, find the last location greater than or equal to it, and do not need to look forward from the end of the array every time. We can change the time complexity to O (n) by improving this location ).

The process is like this. For I, we first find the position j of the largest element on the right side of I and check whether it is better and updated than the optimal solution of the current record. Then, check whether the maximum element position of j + 1 and its right side is greater than or equal to A [I]. If yes, make j equal to this position and repeat the above process. If no, start from position I + 1, but j is still considered from the current position. The reason is described above. In this way, the time complexity is O (n.

For details, refer to the code


[Cpp] pair <int, int> find (const vector <int> &)
{
Int n = A. size ();
If (n = 0)
Throw new invalid_argument ("Array's size can't be 0! ");
 
Vector <int> right_max_pos (n );
Right_max_pos [n-1] = n-1;
For (int I = n-2; I> = 0; -- I)
{
If (A [I]> A [right_max_pos [I + 1])
Right_max_pos [I] = I;
Else
Right_max_pos [I] = right_max_pos [I + 1];
}
 
Int max_len = 0;
Int target_ I, target_j;
Int I = 0, j = 0;
While (j <n)
{
J = right_max_pos [j];
If (A [j]> = A [I])
{
If (j-I + 1> max_len)
{
Target_ I = I;
Target_j = j;
Max_len = j-I + 1;
}
++ J;
}
Else
++ I;
}
 
Return make_pair <int, int> (target_ I, target_j );
}

Pair <int, int> find (const vector <int> &)
{
Int n = A. size ();
If (n = 0)
Throw new invalid_argument ("Array's size can't be 0! ");

Vector <int> right_max_pos (n );
Right_max_pos [n-1] = n-1;
For (int I = n-2; I> = 0; -- I)
{
If (A [I]> A [right_max_pos [I + 1])
Right_max_pos [I] = I;
Else
Right_max_pos [I] = right_max_pos [I + 1];
}

Int max_len = 0;
Int target_ I, target_j;
Int I = 0, j = 0;
While (j <n)
{
J = right_max_pos [j];
If (A [j]> = A [I])
{
If (j-I + 1> max_len)
{
Target_ I = I;
Target_j = j;
Max_len = j-I + 1;
}
++ J;
}
Else
++ I;
}

Return make_pair <int, int> (target_ I, target_j );
}
Here is a brief description of the test method. We can test the simplest implementation scheme first. The first implementation here, because this implementation is simple, the possibility of errors is small, and the test is simple. Time complexity is not considered during testing, but correctness is considered. Then we use the input and output of the tested algorithm to test other algorithms (For results ).

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.