How many OP operations can an array perform in order to order

Source: Internet
Author: User

1 Topic Description:

There is an array: 2,1,4,3. For arrays, there is an operation op (idx): Moves the number corresponding to the index to the first place. 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, the elements of an array are arbitrary integers, there may be 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 requires n times enough, the first time I find the number of the count I in the array, then the OP operation, and finally make the array orderly.

But at least several 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, it must be OP, become 3 2 1 4. If the OP is 3, then the other less than 3 of the number (a) will need to be OP, or not go to the front of 3.

Another example is 2 1 6 3 7 5 4, found that 7, 6 all need op, and starting from 5 need op, then the answer is all the <=5 number. This algorithm needs to be able to find 2 1 6 3 7 5 4 in ascending sequence 6, 7. The complexity is O (n^2), and the spatial 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 last, 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, 8 (this case can only be 10, because 8 appears 2 times, 1 times 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 appearance, then 4 must be OP operation, put to the first, as long as 4 put to the first, then the other 3,2,1,0, such as the number of <=4, all are to be operated. (3 should also be put in the first place, then 2 will be put in the first place, then ...). You can see that only 5, 6, and 73 numbers do not require OP operations. Therefore the total op quantity is n-3 = 8-3 = 5.



To find this sequence, I intend to: traverse from right to left,

IDX: 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, so 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, so Exchange 9 and 5 positions]

8 6 5 7 5 4 9

_min:5 (updated 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 increment sequence: Interchange 4 and 7,p--]

_min:5 (the largest 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 OK, update increment sequence, swap (6, 5), p--]

8 5 5 4 6 7 9 [results after swap]



idx:0, P:4

8 5 5 4 6 7 9 [8 is larger than _min, 8:6 is large, so p keeps growing, knowing that 8 < 9 stops, when P points to 9, such as]

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;

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



Analysis of Complexity:

The scan time is O (n). For swap operations, the last increment sequence is similar to a stack, where each element is up to the stack and is up to O (n). So the time complexity is O (n), and the spatial complexity is O (1).


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How many OP operations can an array perform in order to order

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.