zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Java中的String为什么是不可变的? -- String源码分析
    理解值和对象-快照图
    线程Thread
    Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题?
    不要做一个浮躁的程序员
    关于在springboot中利用@Value注解读取配置文件中的属性值得问题
    Intellij Idea expired
    javascript事件之:谈谈自定义事件
    javascript事件之: 事件冒泡, 事件捕获 ,阻止默认事件
    windows7自带远程连接工具连接不上远程,只有windows服务器版本能连接tsmmc能连接的问题
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/6563290.html
Copyright © 2011-2022 走看看