lintcode-medium-The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j]|) is as small as possible, return their smallest difference.

Example

For example, given array A = [3,6,7,4], B = [2,8,9,3], return 0

Challenge

O(n log n) time

public class Solution {
    /**
     * @param A, B: Two integer arrays.
     * @return: Their smallest difference.
     */
    public int smallestDifference(int[] A, int[] B) {
        // write your code here

        if(A == null || A.length == 0 || B == null || B.length == 0)
            return 0;

        int res = Integer.MAX_VALUE;

        Arrays.sort(B);

        for(int i = 0; i < A.length; i++){

            int left = 0;
            int right = B.length - 1;

            while(left < right - 1){
                int mid = left + (right - left) / 2;

                if(B[mid] == A[i])
                    return 0;

                if(B[mid] > A[i])
                    right = mid;
                else
                    left = mid;
            }

            res = Math.min(res, Math.min(Math.abs(B[left] - A[i]), Math.abs(B[right] - A[i])));
        }

        return res;
    }
}
时间: 2024-12-12 18:46:55

lintcode-medium-The Smallest Difference的相关文章

[lintcode medium]Maximum Subarray Difference

Maximum Subarray Difference Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Example For [1, 2, -3, 1], return 6. Note The subarray should contain at leas

Smallest Difference (poj 2718 暴力枚举)

Language: Default Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5034   Accepted: 1382 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits an

The Smallest Difference

Given two array of integers(the first array is array A, the second array is array B), now we are going to find a element in array A which is A[i], and another element in array B which is B[j], so that the difference between A[i] and B[j] (|A[i] - B[j

POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)

Smallest Difference Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second

POJ2718 Smallest Difference 【贪心+枚举】

Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4639   Accepted: 1290 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in

【POJ - 2718】Smallest Difference(搜索 )

-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数.除非构造的数恰好为0,否则不能以0打头. 举例来说,给定数字0,1,2,4,6与7,你可以写出10和2467.当然写法多样:210和764,204和176,等等.最后一对数差的绝对值为28,实际上没有其他对拥有更小的差. Input  输入第一行的数表示随后测试用例的数量.对于每组测试用例,有一行至少两个

poj 2718 Smallest Difference(穷竭搜索dfs)

Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remaining digits can be written down in some order to form a second integer. Unless the

poj 2718 Smallest Difference

题目大意:给你几个数字,选出一些数字组成一个整数,另外的组成另一个整数,求这两个数差的绝对值的最小值 Input The first line of input contains the number of cases to follow. For each case, there is one line of input containing at least two but no more than 10 decimal digits. (The decimal digits are 0,

Smallest Difference

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5364 Accepted: 1456 Description Given a number of distinct decimal digits, you can form one integer by choosing a non-empty subset of these digits and writing them in some order. The remainin