lintcode-medium-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.

 

/**
 * 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) {
        // write your code here

        if(nuts == null || nuts.length == 0 || bolts == null || bolts.length == 0 || nuts.length != bolts.length)
            return;

        sort(nuts, bolts, 0, nuts.length - 1, compare);

        return;
    }

    public void sort(String[] nuts, String[] bolts, int start, int end, NBComparator compare){

        if(start >= end)
            return;

        int partition = partition(nuts, bolts[end], start, end, compare);
        partition(bolts, nuts[partition], start, end, compare);

        sort(nuts, bolts, start, partition - 1, compare);
        sort(nuts, bolts, partition + 1, end, compare);

        return;
    }

    public int partition(String[] array, String pivot, int start, int end, NBComparator compare){

        if(start >= end)
            return start;

        int left = start;
        int right = end;

        String pivot_mirror = null;

        for(int i = start; i <= end; i++)
            if(compare.cmp(array[i], pivot) == 0 || compare.cmp(pivot, array[i]) == 0){
                pivot_mirror = array[i];
                break;
            }

        while(true){
            while(left < right && ((compare.cmp(array[left], pivot) == -1) || (compare.cmp(pivot, array[left]) == 1)))
                left++;

            while(left < right && ((compare.cmp(array[right], pivot) == 1) || compare.cmp(pivot, array[right]) == -1))
                right--;

            if(left == right)
                break;

            swap(array, left, right);
        }
        array[left] = pivot_mirror;

        return left;
    }

    public void swap(String[] array, int i, int j){
        String temp = array[i];
        array[i] = array[j];
        array[j] = temp;

        return;
    }

};
时间: 2024-10-19 16:33:52

lintcode-medium-Nuts & Bolts Problem的相关文章

Lintcode: Nuts &amp; 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 b

Nuts &amp; 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 b

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium] digit counts

Digit Counts Count the number of k's between 0 and n. k can be 0 - 9. Example if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12) class Solution { /* * param k : As description. * param n : As description. * r

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium]Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return 2147483647 Example Given dividend = 100 and divisor = 9, return 11. public class Solution { /** * @param dividend the dividend

[lintcode medium] Two sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no