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 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的更多相关文章
- [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 ...
- 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 A + B Problem
原题链接在这里:http://www.lintcode.com/en/problem/a-b-problem/ 不让用 数学运算符,就用位运算符. a的对应位 ^ b的对应位 ^ carry 就是re ...
- LintCode "Post Office Problem" !!!
* Non-intuitive state design class Solution { public: /** * @param A an integer array * @param k an ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- 7九章算法强化班全解--------Hadoop跃爷Spark
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- (C#)算法题
1. Convert string from "AAABBCC" to "A3B2C2". 当面试者提出这个问题的时候,首先需要确认题意:譬如:字符串是不是顺序 ...
随机推荐
- ELK对Tomcat日志双管齐下-告警触发/Kibana日志展示
今天我们来聊一聊Tomcat,相信大家并不陌生,tomcat是一个免费开源的web应用服务器,属于轻量级的应用程序,在小型生产环境和并发不是很高的场景下被普遍使用,同时也是开发测试JSP程序的首选.也 ...
- 批量导出hive表的建表语句
转的这里的 首先先导出所有的table表 hive -e "use xxxdb;show tables;" > tables.txt 然后再使用hive内置语法导出hive表 ...
- MariaDB报错Plugin 'InnoDB' init function returned error.解决方案
重新安装MariaDB后,服务一直启动不起来,查看日志有以下错误: InnoDB: No valid checkpoint found. InnoDB: If you are attempting d ...
- CommonJs规范详解---【XUEBIG】
CommonJS是服务器模块的规范,Node.js采用了这个规范 1.CommonJs规范的出发点:JS没有模块系统.标准库较少.缺乏包管理工具:为了让JS可以在任何地方运行,以达到Java.C ...
- 重启部署在阿里云上的huginn
背景,因为重新编译安装了gcc,不知怎么服务器上的huginn就停了, 因为之前是安装在docker上的,服务器重启之后需要:1.启动dockerservice docker start2.命令创建h ...
- CF 960 G
难受的1b,怎么会这样 先去学写一发 NTT 大概说一下斯特林数
- [POI2012]Odległość
[POI2012]Odległość 题目大意: 一个长度为\(n(n\le10^5)\)的序列\(A(1\le A_i\le10^6)\),定义\(d(i,j)\)为每次对\(A_i,A_j\)中的 ...
- XIV Open Cup named after E.V. Pankratiev. GP of Europe
A. The Motorway 等价于找到最小和最大的$L$满足存在$S$使得$S+(i-1)L\leq a_i\leq S+i\times L$ 即 $S\leq\min((1-i)L+a_i)$ ...
- centos7中安装pg数据库
# centos中安装的命令 # yum install postgresql-server.x86_64 # 安装之前可以通过以下命令价差是否已经安装过 rpm -qa | grep postgre ...
- 问题10:获取当前页面宽度JS
var mleft=document.getElementById("mleft"); var h = document.documentElement.clientHeight; ...