Nuts & Bolts Problem
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 nuts = ['ab','bc','dd','gg'], bolts = ['AB','GG', 'DD', 'BC'].
Your code should find the matching bolts and nuts.
one of the possible return:
nuts = ['ab','bc','dd','gg'], bolts = ['AB','BC','DD','GG'].
we will tell you the match compare function. If we give you another compare function.
the possible return is the following:
nuts = ['ab','bc','dd','gg'], bolts = ['BC','AA','DD','GG'].
So you must use the compare function that we give to do the sorting.
The order of the nuts or bolts does not matter. You just need to find the matching bolt for each nut.
There is a solution to this problem in O(nlgn) that works even if you can't compare two nuts or two bolts directly. That is without using a scale, or if the differences in weight are too small to observe.
It works by applying a small variation of randomised quicksort as follows:
Pick a random bolt b.
Compare bolt b with all the nuts, partitioning the nuts into those of size less than b and greater than b.
Now we must also partition the bolts into two halves as well and we can't compare bolt to bolt. But now we know what is the matching nut n to b. So we compare n to all the bolts, partitioning them into those of size less than n and greater than n.
The rest of the problem follows directly from randomised quicksort by applying the same steps to the lessThan and greaterThan partitions of nuts and bolts.
从这题得到的启发:如果一个本需要用O(N^2)来解决的问题,而被要求用更低的复杂度,应该想到需要减少compare的次数,怎么减少? partiton.
/**
* public class NBCompare {
* public int cmp(String a, String b);
* }
* You can use compare.cmp(a, b) to compare nuts "a" and bolts "b",
* if "a" is bigger than "b", it will return 1, else if they are equal,
* it will return 0, else if "a" is smaller than "b", it will return -1.
* When "a" is not a nut or "b" is not a bolt, it will return 2, which is not valid.
*/
public class Solution { public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) {
// write your code here
if (nuts == null || bolts == null || nuts.length != bolts.length || compare == null) return;
helper(nuts, bolts, , nuts.length - , compare);
} public void helper(String[] nuts, String[] bolts, int start, int end, NBComparator c) {
if (start < end) {
int p = partition(nuts, bolts, start, end, c);
helper(nuts, bolts, start, p - , c);
helper(nuts, bolts, p + , end, c);
}
} public int partition(String[] nuts, String[] bolts, int start, int end, NBComparator compare) {
int p = start;
for (int i = start; i < end; i++) {
if (compare.cmp(nuts[i], bolts[end]) < ) {
swap(nuts, p, i);
p++;
} else if (compare.cmp(nuts[i], bolts[end]) == ) {
swap(nuts, i, end);
i--;
}
} swap(nuts, p, end); int k = start;
for (int i = start; i < end; i++) {
if (compare.cmp(nuts[p], bolts[i]) > ) {
swap(bolts, k, i);
k++;
} else if (compare.cmp(nuts[p], bolts[i]) == ) {
swap(bolts, i, end);
i--;
}
}
swap(bolts, k, end); return k;
} public void swap(String[] arr, int i, int j) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
};
Nuts & Bolts Problem的更多相关文章
- 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 ...
- Lintcode399 Nuts & Bolts Problem solution 题解
[题目描述] Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one m ...
- [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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 7九章算法强化班全解--------Hadoop跃爷Spark
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- (C#)算法题
1. Convert string from "AAABBCC" to "A3B2C2". 当面试者提出这个问题的时候,首先需要确认题意:譬如:字符串是不是顺序 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...
- 使用k8s-prometheus-adapter实现HPA
环境: kubernetes 1.11+/openshift3.11 自定义metric HPA原理: 首选需要注册一个apiservice(custom metrics API). 当HPA请求me ...
随机推荐
- think in UmL(三)
在实践中思考! 在这一部分中,书中作者用实际的案例讲述了从一个个实际项目的可行性分析阶段倒是现阶段的整个过程,让我们奖赏部分学到的UML知识点在实践中的得到学习. 当我们拿到一个项目的时候首先要做的就 ...
- 第一个spring冲刺总结及后诸葛亮报告(附团队贡献分)
眨眼就完结了第一阶段的冲刺了,之前因为学校停电停水等诸多原因而导致冲刺完毕时间的推迟. 第一阶段总体是做到了运算的功能,只是一些基本的功能实现,但能保证的容错性能较高. 1.在普遍的四则运算中都能见到 ...
- Zero-shot learning(零样本学习)
一.介绍 在传统的分类模型中,为了解决多分类问题(例如三个类别:猫.狗和猪),就需要提供大量的猫.狗和猪的图片用以模型训练,然后给定一张新的图片,就能判定属于猫.狗或猪的其中哪一类.但是对于之前训练图 ...
- Software-Defined Networking:A Comprehensive Survey--Day1
Software-Defined Networking:A Comprehensive Survey 摘要: 传统网络复杂且难以管理,根据预定义策咯也难以对网络进行配置,也难以重新配置. 软件定义网络 ...
- 用prop还是attr
jquery中attr和prop的区别 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答 ...
- [转帖] Windows 与linux的栈大小问题
一般来说,我们所用的内存有栈和堆之分,其它的我们很少控制,栈的速度快,但是空间小.不灵活:而堆的空间几乎可以满足任何要求.灵活,但是相对的速度要慢了很多,并且在VC中堆是人为控制的,new了就要del ...
- day3——关于<s:if/>和文件上传
一个小的注意点 <s:if test='#backyear==#yearatd'>selected="selected"</s:if> <s:if t ...
- wordpress WP-PageNavi分页
1.安装WP-PageNavi分页插件: 这个就没什么好介绍直接安装插件. 2.在需要分页的页面按下面的方式加上相应代码: 插入的位置在以 <?php if (have_posts()) : ? ...
- C# DataTable Select用法
DataRow[] dr = ds.Tables[0].Select("列名='该列你要查询的值'"); DataRow[] dr = ds.Tables[0].Select(&q ...
- win10用vncviewer远程登陆ubuntu桌面
一:安装Ubuntu的服务端桌面环境 # 安装xrdpsudo apt-get install xrdp # 安装xfce4sudo apt-get updatesudo apt-get instal ...