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.

Example

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.


Analysis: http://www.geeksforgeeks.org/nuts-bolts-problem-lock-key-problem/

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的更多相关文章

  1. 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 ...

  2. 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 ...

  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. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. 7九章算法强化班全解--------Hadoop跃爷Spark

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  6. (C#)算法题

    1. Convert string from "AAABBCC" to "A3B2C2". 当面试者提出这个问题的时候,首先需要确认题意:譬如:字符串是不是顺序 ...

  7. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  8. 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 ...

  9. 使用k8s-prometheus-adapter实现HPA

    环境: kubernetes 1.11+/openshift3.11 自定义metric HPA原理: 首选需要注册一个apiservice(custom metrics API). 当HPA请求me ...

随机推荐

  1. 20135337朱荟潼 Linux第六周学习总结——进程的描述和进程的创建

    朱荟潼 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课http://mooc.study.163.com/course/USTC 1000029000 第六周 进程的描述 ...

  2. C#Dictionary使用记录

    一.区别 在工作中经常遇到C#数组.ArrayList.List.Dictionary存取数据,其区别和优劣势为: 初始化 数组: int[] buff = new int[6]; ArrayList ...

  3. Git 笔记——如何处理分支合并冲突

    1.前言 学习使用 Git 也有一段时间,但一直都是把 Git 当作一个代码仓库,使用的命令无非就是 clone, add, commit ,往往课程作业也没有过多人合作开发,没有体验过 Git 的分 ...

  4. Visual Studio 2013安装及简单的单元测试

    Visual Studio 2013安装部分首先到观望下载了英文版和简体中文的语言包.但是更新语言包时总是失败,安装vs需要等待的时间也很大.后来重新装了一遍系统(这个卸载也很麻烦),从其他网站下了中 ...

  5. CentOS 简单学习 firewalld的使用

    1. centos7 开始 使用firewalld 代替了 iptables 命令工具为 firewall-cmd 帮助信息非常长,简单放到文末 2. 简单使用 首先开启 httpd 一般都自带安装了 ...

  6. [转帖]在VMware ESXi服务器上配置NAT上网 需要学习一下。

    http://blog.51cto.com/boytnt/1292487 在使用VMware workstation的时候,我们经常以NAT的方式配置虚拟机的网络,与桥接方式相比,这样配置可以让虚拟机 ...

  7. webpack 搭建vue项目流程

    1.安装node 2.打开命令行输入  npm install -g vue-cli 3.vue init webpack-simple vue 4.各种确认(enter键) 5.npm instal ...

  8. java8新特性(三)_Optional类的使用

    说实话,我第一次知道这个东西是从阿里规约中,因为公司前一段时间一直在搞代码审核,我的代码写的就感觉很烂,就像规范下.让别人看起来没那么烂.于是就开始看阿里规约,在看到NPE处理的时候,上面提到用Opt ...

  9. BZOJ4559 JLOI2016成绩比较(容斥原理+组合数学+斯特林数)

    容斥一发改为计算至少碾压k人的情况数量,这样对于每门课就可以分开考虑再相乘了.剩下的问题是给出某人的排名和分数的值域,求方案数.枚举出现了几种不同的分数,再枚举被给出的人的分数排第几,算一个类似斯特林 ...

  10. Nastya and a Wardrobe CodeForces - 992C(规律)

    写一下二叉树  推一下公式就出来了, 注意取模时的输出形式 #include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a ...