【题目描述】

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.

给定一组 n 个不同大小的 nuts 和 n 个不同大小的 bolts。nuts 和 bolts 一一匹配。 不允许将 nut 之间互相比较,也不允许将 bolt 之间互相比较。也就是说,只许将 nut 与 bolt 进行比较, 或将 bolt 与 nut 进行比较。请比较 nut 与 bolt 的大小。

【题目链接】

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

【题目解析】

螺帽螺母问题脱胎于排序问题,这里的特别之处在于需要通过两个array进行对应的排序。这就需要利用一个array中的元素对另一个array进行partition,并反过来重复这一个过程,最终让两个array都满足comparator所定义的相同顺序。

这里要注意的是partition的变式,因为pivot并非来源当前array,只能通过一方元素作为基准,对另一个array进行partition。

核心在于:首先使用 nuts 中的某一个元素作为基准对 bolts 进行 partition 操作,随后将 bolts 中得到的基准元素作为基准对 nuts 进行 partition 操作。

【参考答案】

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

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Lintcode399 Nuts & Bolts Problem solution 题解的更多相关文章

  1. Nuts & Bolts Problem

    Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...

  2. Lintcode: Nuts & Bolts Problem

    Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...

  3. [LintCode] Nuts & Bolts Problem 螺栓螺母问题

    Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...

  4. 区分DDD中的Domain, Subdomain, Bounded Context, Problem/Solution Space

    区分DDD中的Domain, Subdomain, Bounded Context, Problem/Solution Space 译自: Domain, Subdomain, Bounded Con ...

  5. CF742B Arpa's obvious problem and Mehrdad's terrible solution 题解

    Content 有一个长度为 \(n\) 的数组,请求出使得 \(a_i \oplus a_j=x\) 且 \(i\neq j\) 的数对 \((i,j)\) 的个数.其中 \(\oplus\) 表示 ...

  6. BZOJ2298:[HAOI2011]problem a——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2298 https://www.luogu.org/problemnew/show/P2519 一次 ...

  7. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  8. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

  9. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

随机推荐

  1. Struts2 源码剖析 控制部分-----1

    这部分着重分析从我们发出一个uri请求,一直到代码运行到我们自己写的action类为止,struts的控制部分的代码(还有数据流部分,我们后面再分析) 已经用了快1年多的struts2了,一直认为对开 ...

  2. leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  3. NSData 与 struct 以及XML的转换。

    在做OC与C++ 混编的时候,我们可能会用到struct 与NSData的相互转换.在这里做一个记录 1.struct转换为NSData 例如如下的struct: struct tagPackageH ...

  4. 小强的HTML5移动开发之路(6)——Canvas图形绘制基础

    来自:http://blog.csdn.net/dawanganban/article/details/17686039 在前面提到Canvas是HTML5中一个重要特点,canvas功能非常强大,用 ...

  5. Cocos2D遍历场景图(Scene Graph)

    另一个Cocos2D有用的调试特性是打印出递归的打印出节点的孩子们. 你可以添加以下一行到MainScene或GameScene的didLoadFromCCB的方法中: [self.scene wal ...

  6. 22_Android中的本地音乐播放器和网络音乐播放器的编写,本地视频播放器和网络视频播放器,照相机案例,偷拍案例实现

    1 编写以下案例: 当点击了"播放"之后,在手机上的/mnt/sdcard2/natural.mp3就会播放. 2 编写布局文件activity_main.xml <Line ...

  7. Android群英传笔记——第十章:Android性能优化

    Android群英传笔记--第十章:Android性能优化 随着Android应用增多,功能越来越复杂,布局也越来越丰富了,而这些也成为了阻碍一个应用流畅运行,因此,对复杂的功能进行性能优化是创造高质 ...

  8. 【Qt编程】Qt学习笔记<二>

    1.      QApplication类和QWidget类都包含在QtGui模块中.所以我们可以只包含这个头文件即可. 2.      在c++中,用new分配了内存空间就需要用delete来释放空 ...

  9. Unity3D学习笔记(五)C#与JavaScript组件访问的比较

    由于之前用JavaScript用的比较多,因此总是想用以前的方法来访问组件,却屡遭失败,经过查阅资料发现,二者存在较大的不同. 下面以调用3D Text组件HurtValue为例,来比较二者的不同 J ...

  10. java数据类型易错点简单总结,欢迎大神前辈补充!谢谢

    数据类型那这边看似简单,花了我很长时间也就是才练到几成"功力"吧.还希望路过的大神在下面补充,菜鸟的我深受感谢! 首先看两个思考题 思考题1:请问下面这个有没有问题 double ...