• 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

     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 integers
    13      * @param bolts: an array of integers
    14      * @param compare: a instance of Comparator
    15      * @return: nothing
    16      */
    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 recursively
    32         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 }
  • 相关阅读:
    Array中数据强制数据类型转换
    去除socket编程当中接收到的多余符\0
    <转>在 ASP.NET 中执行 URL 重写
    小牛生产小牛的问题解决集粹
    SAP ABAP鸟瞰【AV+PPT】
    cx_Oracle说:Python访问Oracle并不难
    resolve.conf引起登录HPUX的CDE故障
    HPUX 11i v2安装使用python 2.5.2
    HPUX下使用python发送邮件
    HPUX 11i v2上Oracle10.2基本安装指南
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6563290.html
Copyright © 2020-2023  润新知