Lintcode399 Nuts & Bolts Problem solution 题解

【Title description】

Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with nut to see which one is bigger/smaller.

We will give you a compare function to compare nut with bolt.

Given a set of n nuts of different sizes and bolts of n different sizes. Nuts and bolts are matched one by one. Comparing nuts with each other is not allowed, nor is it allowed to compare bolts with each other. That is, you can only compare nut to bolt, or bolt to nut. Please compare the size of nut and bolt.

 

【Title link】

www.lintcode.com/en/problem/nuts-bolts-problem/

 

【Topic Analysis】

The nut and nut problem is born out of the sorting problem. The special thing here is that the corresponding sorting needs to be performed through two arrays. This requires using the elements in one array to partition another array, and repeating this process in turn, so that both arrays meet the same order defined by the comparator.

What should be noted here is the variant of partition, because pivot is not the source of the current array, and only one element can be used as a benchmark to partition another array.

The core is: first use an element in the nuts as the benchmark to perform the partition operation on the bolts, and then use the benchmark element obtained in the bolts as the benchmark to perform the partition operation on the nuts.

 

【Reference answer】

www.jiuzhang.com/solutions/nuts-bolts-problem/


Copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326071871&siteId=291194637