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.

Quick Sort Way: We can use quick sort technique to solve this. We represent nuts and bolts in character array for understanding the logic.

Nuts represented as array of character
char nuts[] = {‘@’, ‘#’, ‘$’, ‘%’, ‘^’, ‘&’}

Bolts represented as array of character
char bolts[] = {‘$’, ‘%’, ‘&’, ‘^’, ‘@’, ‘#’}

This algorithm first performs a partition by picking last element of bolts array as pivot, rearrange the array of nuts and returns the partition index ‘i’ such that all nuts smaller than nuts[i] are on the left side and all nuts greater than nuts[i] are on the right side. Next using the nuts[i] we can partition the array of bolts. Partitioning operations can easily be implemented in O(n). This operation also makes nuts and bolts array nicely partitioned. Now we apply this partitioning recursively on the left and right sub-array of nuts and bolts.

As we apply partitioning on nuts and bolts both so the total time complexity will be Θ(2*nlogn) = Θ(nlogn) on average.

Partition function类似sort colors

 /**
* 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 {
/**
* @param nuts: an array of integers
* @param bolts: an array of integers
* @param compare: a instance of Comparator
* @return: nothing
*/
public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) {
if (nuts == null || bolts == null) return;
if (nuts.length != bolts.length) return; qsort(nuts, bolts, compare, 0, nuts.length - 1);
} private void qsort(String[] nuts, String[] bolts, NBComparator compare,
int l, int u) {
if (l >= u) return;
// find the partition index for nuts with bolts[u]
int part_inx = partition(nuts, bolts[u], compare, l, u);
// partition bolts with nuts[part_inx]
partition(bolts, nuts[part_inx], compare, l, u);
// qsort recursively
qsort(nuts, bolts, compare, l, part_inx - 1);
qsort(nuts, bolts, compare, part_inx + 1, u);
} private int partition(String[] str, String pivot, NBComparator compare,
int l, int u) { int low = l;
int high = u;
int i = low;
while (i <= high) {
if (compare.cmp(str[i], pivot) == -1 ||
compare.cmp(pivot, str[i]) == 1) {
swap(str, i, low);
i++;
low++;
}
else if (compare.cmp(str[i], pivot) == 1 ||
compare.cmp(pivot, str[i]) == -1) {
swap(str, i, high);
high--;
}
else i++;
}
return low;
} private void swap(String[] str, int l, int r) {
String temp = str[l];
str[l] = str[r];
str[r] = temp;
}
}

Lintcode: 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. 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 A + B Problem

    原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是re ...

  5. LintCode "Post Office Problem" !!!

    * Non-intuitive state design class Solution { public: /** * @param A an integer array * @param k an ...

  6. [LintCode]——目录

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

  7. lintcode算法周竞赛

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

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

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

  9. (C#)算法题

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

随机推荐

  1. 将cookie 转换成字典格式

    b = 'bid=Qzw9cKnyESM; ll="108288"; __yadk_uid=4YChvgeANLBEh4iV00n1tc0HQ8zpmSl1; __utmc=301 ...

  2. C# 数据为空,不能对NULL调用此方法或属性的解决办法

    在运行C#项目时,报出了以下错误,错误原因是数据库中的值为null时,查询时会触发该错误提示 部分源代码如下: public List<Student> findData2() { ; / ...

  3. 取消layUI中日期选择控件默认填充日期

    input标签中使用日期选择控件填写,加载时默认填充当前日期, 标签设置了placeholder="请选择" autocomplete="off",但是并没有效 ...

  4. rem实现移动端自适应页面

    一.把px转换成rem方案 1.cssrem插件 2.css预处理器 3.rem-unit插件 4.px2rem插件 rem就是相对于根元素的font-size来做计算,设置好根结点字体大小,子节点用 ...

  5. 【AtCoder】【思维】【置换】Rabbit Exercise

    题意: 有n只兔子,i号兔子开始的时候在a[i]号位置.每一轮操作都将若干只兔子依次进行操作: 加入操作的是b[i]号兔子,就将b[i]号兔子移动到关于b[i]-1号兔子现在所在的位置对称的地方,或者 ...

  6. Django——发送邮件

    Django--发送邮件 在web应用中,服务器对客户发送邮件来通知用户一些信息,可以使用邮件来实现. Django中提供了邮件接口,使我们可以快捷的建设一个邮件发送系统. 以下是一个简单实例: se ...

  7. ECMA Script 6_模块加载方案 ES6 Module 模块语法_import_export

    1. 模块加载方案 commonJS 背景: 历史上,JavaScript 一直没有模块(module)体系, 无法将一个大程序拆分成互相依赖的小文件,再用简单的方法拼装起来. 其他语言都有这项功能: ...

  8. svn的简单学习与日常使用

  9. tomcat体系结构

    总体架构解析 Server: 一个StandardServer类实例就表示一个Server容器,TOMCAT启动的时候首先会启动一个Server,一个Server包括多个Service Service ...

  10. HMAC-SHA256 签名方法各个语音的实现方式之前端JavaScriptes6

    sha256和16进制输出,网上很多种后端的验证方法,几乎没有前端的,所以自己写了个,希望给类似需求的人一个帮助,适用场景 腾讯云接口鉴权 v3签名 npm install sha256npm ins ...