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.

Using the function we give you, you are supposed to sort nuts or bolts, so that they can map in order.

Example

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

Your code should find the matching of bolts and nuts.

According to the function, one of the possible return:

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

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.

思路:算法比较好想,就是用A最左边的一个pivot, 去sort B,然后用B[index] = A[l]  ,去sort A,然后recursion;每次用start,去sort另外一个数组,首先把match的放到B的头部,然后partition后面的部分,partition完了之后,把start换到j的位子,这样才是一个正确的顺序;

/**
 * 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) {
        int n = nuts.length;
        sort(nuts, bolts, compare, 0, n - 1);
    }
    
    private void sort(String[] nuts, String[] bolts, NBComparator compare, int start, int end){
        if(start >= end) {
            return;
        }
        String pivot = bolts[start];
        int index = partition(nuts, compare, start, end, pivot);
        partition(bolts, compare, start, end, nuts[index]);
        sort(nuts, bolts, compare, start, index - 1);
        sort(nuts, bolts, compare, index + 1, end);
    }
    
    private int partition(String[] strs, NBComparator compare, 
                            int start, int end, String pivot) {
        for(int i = start; i <= end; i++) {
            if(compare.cmp(strs[i], pivot) == 0 || compare.cmp(pivot, strs[i]) == 0) {
                swap(strs, start, i);
                break;
            }
        }
        
        int i = start + 1; int j = end;
        while(i <= j) {
            while(i <= j && (compare.cmp(strs[i], pivot) == -1 || compare.cmp(pivot, strs[i]) == 1)) {
                i++;
            }
            while(i <= j && (compare.cmp(strs[j], pivot) == 1 || compare.cmp(pivot, strs[j]) == -1)) {
                j--;
            }
            if(i <= j) {
                swap(strs, i, j);
                i++;
                j--;
            }
        }
        swap(strs, start, j);  // 一定要把头部换到j,这样才是一个正确的顺序;
        return j;
    }
    
    private void swap(String[] A, int i, int j) {
        String temp = A[i];
        A[i] = A[j];
        A[j] = temp;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值