博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lintcode: Nuts & Bolts Problem
阅读量:4542 次
发布时间:2019-06-08

本文共 3716 字,大约阅读时间需要 12 分钟。

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

1 /** 2  * public class NBCompare { 3  *     public int cmp(String a, String b); 4  * } 5  * You can use compare.cmp(a, b) to compare nuts "a" and bolts "b", 6  * if "a" is bigger than "b", it will return 1, else if they are equal, 7  * it will return 0, else if "a" is smaller than "b", it will return -1. 8  * When "a" is not a nut or "b" is not a bolt, it will return 2, which is not valid. 9 */10 public class Solution {11     /**12      * @param nuts: an array of integers13      * @param bolts: an array of integers14      * @param compare: a instance of Comparator15      * @return: nothing16      */17     public void sortNutsAndBolts(String[] nuts, String[] bolts, NBComparator compare) {18         if (nuts == null || bolts == null) return;19         if (nuts.length != bolts.length) return;20 21         qsort(nuts, bolts, compare, 0, nuts.length - 1);22     }23 24     private void qsort(String[] nuts, String[] bolts, NBComparator compare, 25                        int l, int u) {26         if (l >= u) return;27         // find the partition index for nuts with bolts[u]28         int part_inx = partition(nuts, bolts[u], compare, l, u);29         // partition bolts with nuts[part_inx]30         partition(bolts, nuts[part_inx], compare, l, u);31         // qsort recursively32         qsort(nuts, bolts, compare, l, part_inx - 1);33         qsort(nuts, bolts, compare, part_inx + 1, u);34     }35 36     private int partition(String[] str, String pivot, NBComparator compare, 37                           int l, int u) {38         39         int low = l;40         int high = u;41         int i = low;42         while (i <= high) {43             if (compare.cmp(str[i], pivot) == -1 || 44                 compare.cmp(pivot, str[i]) == 1) {45                 swap(str, i, low);46                 i++;47                 low++;48             }49             else if (compare.cmp(str[i], pivot) == 1 || 50                 compare.cmp(pivot, str[i]) == -1) {51                 swap(str, i, high);52                 high--;53             }54             else i++;55         }56         return low;57     }58 59     private void swap(String[] str, int l, int r) {60         String temp = str[l];61         str[l] = str[r];62         str[r] = temp;63     }64 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6563290.html

你可能感兴趣的文章
C++重载运算符练习--对people类重载“= =”运算符和“=”运算符
查看>>
Nmap命令的实用范例
查看>>
7-1 查找整数编程总结
查看>>
安装PHP以及搭建博客(一)
查看>>
关于WORD文档的读取乱码问题
查看>>
[问题记录.dotnet]取网卡信息报错"找不到"-WMI - Not found
查看>>
Codeforces Round #254 (Div. 2):B. DZY Loves Chemistry
查看>>
[svc][cpu][jk]cpu的核心查看及什么是cpu的负载
查看>>
vue基础课堂一
查看>>
Socket实现原理和机制
查看>>
linux 安装虚拟机
查看>>
7bit ASCII编解码
查看>>
flask-sqlalchemy(包含离线脚本,with在上下文管理的应用)
查看>>
机器学习工程师 - Udacity 强化学习 Part Ten
查看>>
go语言 新手学习笔记 go基础教程
查看>>
zabbix 添加宏变量
查看>>
2016年11月1日——jQuery源码学习笔记
查看>>
Thinkphp5笔记二:创建模块
查看>>
centos 安装mysql
查看>>
Redis 禁用FLUSHALL FLUSHDB KEYS 命令
查看>>