zoukankan      html  css  js  c++  java
  • 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.

    Example

    Given nuts = ['ab','bc','dd','gg'], bolts = ['AB','GG', 'DD', 'BC']

    Your code should find the matching bolts and nuts. 

    one of the possible return:

    nuts = ['ab','bc','dd','gg'], bolts = ['AB','BC','DD','GG'].

    we will tell you the match compare function. If we give you another compare function.

    the possible return is the following:

    nuts = ['ab','bc','dd','gg'], bolts = ['BC','AA','DD','GG'].

    So you must use the compare function that we give to do the sorting.

    The order of the nuts or bolts does not matter. You just need to find the matching bolt for each nut.

    Lintcode上的一道题目,本身题目来源是geeksforgeeks。即是对两个数组进行配对排序,但是每个数组的自身元素不可以进行比较。

    如果使用暴力方法,取其中一个数组,对该数组中的每个元素再另外一个数组中进行排序,则复杂度是O(n^2)的。优化方法是使用快速排序。即每次先用bolts数组的最后一个元素对nuts进行一个partition。则partition得到的中间元素就是和bolts数组最后一个元素对应的元素。用这个元素再对bolts元素做排序,则nuts和bolts数组中的划分位的元素是一对,且在数组中的位置一样。之后根据这个位置信息做递归进行循环处理。是一个交叠的快速排序过程。时间复杂度O(nlogn),代码如下:

    # class Comparator:
    #     def cmp(self, a, 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.
    class Solution:
        # @param nuts: a list of integers
        # @param bolts: a list of integers
        # @param compare: a instance of Comparator
        # @return: nothing
        def sortNutsAndBolts(self, nuts, bolts, compare):
            if not nuts or not bolts:
                return
            if len(nuts) != len(bolts):
                return 
            self.quicksort(nuts, bolts, 0, len(nuts)-1, compare)
            return 
        
        def quicksort(self, nuts, bolts, low, high, compare):
            if low < high:
                index = self.partition(nuts, low, high, bolts[high], compare) #get the pivot nuts's index 
                self.partition(bolts, low, high, nuts[index], compare)  #the positions get aligned
                self.quicksort(nuts, bolts, low, index-1, compare)
                self.quicksort(nuts, bolts, index + 1, high, compare)
                
                
        def partition(self, arr,low, high, pivot, compare):
            i = low - 1
            j = low 
            while j < high:
                state1 = compare.cmp(arr[j], pivot)
                state2 = compare.cmp(pivot, arr[j])
                if state1 == -1 or state2 == 1:
                    i += 1
                    if i != j:
                        arr[i], arr[j] = arr[j], arr[i]
                    j += 1
                elif state1 == 0 or state2 == 0:
                    arr[j], arr[high] = arr[high], arr[j]
                else:
                    j +=1 
            i += 1
            arr[i], arr[high] = arr[high], arr[i]
            
            return i        

    值的注意的是compare类的cmp函数比的是nuts和bolts,而每次partition针对的是其中一个数组,所以pivot和数组不能明确知道哪个是nuts,哪个是bolts,需要都比较一下。

  • 相关阅读:
    css实现鼠标悬浮字体流光背景模糊效果
    原生JS实现省市区(县)三级联动选择
    多线程的对象锁和类锁
    session、cookie与“记住我的登录状态”的功能的实现
    Java NIO FileVisitor 高效删除文件
    mysql 服务启动失败
    Http 协议详解
    设计模式 之 策略模式
    简单探讨 javascript 闭包
    数据库SQL优化大总结之 百万级数据库优化方案
  • 原文地址:https://www.cnblogs.com/sherylwang/p/5656703.html
Copyright © 2011-2022 走看看