How many OP operations does an array perform in order to

Source: Internet
Author: User

1 Topic Description:

There is an array: 2,1,4,3. For an array, there is an operation op (idx): Moves the number corresponding to the index to the first. Like what:

OP (3): 2 1-> 3 2 1 4

OP (1): 3 2-> 2 3 1 4
OP (2): 2 3 1 4-> 1 2 3 4

Q for a given array, each element of the array is an arbitrary integer, possibly with duplicate values, and how many OP operations are required to make the array orderly.

For the above example, it takes 3 times.

2. Problem Resolution:

The worst case needs n is enough, the first time I find the number of the penultimate in the array, and then the OP operation, the final will make the array is ordered.

But at least a few times, how to solve.

2.1 Time: O (n^2) space: O (1)

For 2 1 4 3,4 certainly do not need op, next for 3, must be OP, into 3 2 1 4. If the OP is 3, then the other number less than 3 (1,2) needs to be OP, otherwise you can't run to the front of 3.

Another example is 2 1 6 3 7 5 4, found that 7, 6 need op, and starting from 5 need to do op, then the answer is the number of all <=5. This algorithm needs to be able to find 2 1 6 3 7 5 4 increment sequence 6, 7. The complexity is O (n^2), and the space complexity is O (1).

2.2 Time: O (n) Space: O (1)

8 6 5 7 9 5 4

Goal: To find the largest number, the second largest number, the third largest number, and the largest number in the end, the other several numbers in the front. Like what

8 6 5 7 9 5 4-> -8 6 5 7 9 5 4

5 2 1 6 3 7 0 4-> , 5 2 1 6 3 7 0 4

3 4 8 8-> 3 4 8 ten 8 (in this case only 10, because 8 occurs 2 times, of which 1 are still behind 10, so exclude 8)

When this increment sequence is found, for example: 5 2 1 6 3 7 0 4-> 5 2 1 6 3 7 0 4. Then I know 41 set in 5 after the show, then 4 must carry OP operation, put in the first place, as long as 4 put the first, then other 3,2,1,0 and other <=4 number, are all to operate. (3 also want to put in the first place, then 2 also want to put first, then ...). You can see that only 5, 6, 73 numbers do not require OP operations. Therefore the total op quantity is n-3 = 8-3 = 5.



To find this sequence, I'm going to: traverse from right to left,

IDX: For the pointer to traverse; p: The lowest bit pointer of the increment sequence above, _min: Record the maximum value between Arr[idx, P-1]. In the traversal process, the increment sequence is constantly moved to the right, such as

Idx:6, P:6,

8 6 5 7 9 5 4

_min:uninitialzed



Idx:5, P:6,

8 6 5 7 9 5 4 [5:4 Large, therefore Exchange 5 and 4 positions]

8 6 5 7 9 4 5

_min:4 (_min update to 4)

Idx:4, P:6

8 6 5 7 9 4 5 [9:5 Large, therefore exchange 9 and 5 positions]

8 6 5 7 5 4 9

_min:5 (update to the largest of 5 and 4)



Idx:3, P:6

8 6 5 7 5 4 9

8 6 5 4 5 7 9 [7:9 Small, so 7 can be included in the ascending sequence: Exchange 4 and 7,p--]

_min:5 (the biggest in 4,5)



Idx:2, P:5

8 6 5 4 5 7 9 [due to 5 <= _min (5), so direct idx--]

Idx:1, P:5

8 6 5 4 5 7 9 [6 greater than _min, so is OK, update increment sequence, swap (6, 5), p--]

8 5 5 4 6 7 9 [Result of swap]



idx:0, P:4

8 5 5 4 6 7 9 [8 is bigger than _min, 8:6 is big, so p keeps growing, knowing that 8 < 9 just stops, when P points to 9, the following figure]

8 5 5 4 6 7 9 [8:9 Small, so swap (8, 7)]

7 5 5 4 6 8 9 [result]



IDX:-1, P:5

-> break;

So the maximum increment sequence is the length of 2,{8, 9}. So return n-2 is 7-2 = 5



Complexity Analysis:

The time of the scan is O (n). For swap, the last increment sequence is similar to a stack, with each element up to the stack one time, and the most O (n). So the time complexity is O (n), and the space complexity is O (1).


3, more excellent algorithm

The time complexity of this algorithm is O (n), and the space complexity is O (1).

Concrete Ideas:8 6 5 7 9 4. From left to right to find the stage of the mountainside (8,10), and then the valley between the mountain and the back are removed (6,5,7, as well as 9,4)

There are P1, p2 two pointers, p1 pointing to 8,p2 pointing to 6, smaller than 8, and then moving right. Point to 5, or less than 8, then move right. Finally pointed 10, at this time P1 also the updating points to 10. Tablets

Continue to go from 9 onwards.

P1 points Over (8,10), the largest of the P2 points (6,5,7,9,4) P2 is 9, so it is the final answer to exclude the P1 point (8,10) that is smaller than 9. The elimination process is: P1,P2 Repeat the above process, but now need to record P1 point to the number of elements than 9, this is the final answer.

The above method, write code simple, feasible. The complexity of time space is also possible.

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.