387-最小差

给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|)。返回最小差。

样例

给定数组 A = [3,4,6,7], B = [2,3,8,9],返回 0。

挑战

时间复杂度 O(n log n)

标签

两根指针 数组 LintCode 版权所有 排序

思路

一次遍历 + 二分查找,首先遍历数组 A,之后以 A[i] 为目标,对数组 B 进行二分查找,找出 B 中与 A[i] 的最小差

code

class Solution {
public:
/**
* @param A, B: Two integer arrays.
* @return: Their smallest difference.
*/
int smallestDifference(vector<int> &A, vector<int> &B) {
// write your code here
int sizeA = A.size(), sizeB = B.size();
if (sizeA <= 0 || sizeB <= 0) {
return 0;
} int result = INT_MAX;
sort(B.begin(), B.end());
for (int i = 0; i < sizeA; i++) {
int low = 0, high = sizeB - 1, target = A[i], mid = 0;
while (low <= high) {
mid = low + (high - low) / 2;
if (B[mid] == target) {
return 0;
}
else if (B[mid] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
result = min(result, abs(A[i] - B[mid]));
if(mid > 0) {
result = min(result, abs(A[i] - B[mid - 1]));
}
if (mid < sizeB - 1) {
result = min(result, abs(A[i] - B[mid + 1]));
}
}
return result;
}
};

lintcode-387-最小差的更多相关文章

  1. LintCode 387: Smallest Difference

    LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...

  2. [lintcode the-smallest-difference]最小差(python)

    题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 ...

  3. lintcode:最小差

    最小差 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. ...

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  6. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  7. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  8. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  9. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

随机推荐

  1. error:0906D064:PEM routines:PEM_read_bio:bad base64 decode

    今天在使用easywechat对接企业打款到银行卡时,遇到了两个错误 error:0906D064:PEM routines:PEM_read_bio:bad base64 decode 和 erro ...

  2. 我一个自己的关于II和&&的逻辑判断(傻逼型)

    原因 首先概述下起始原因:本来埋点的数据中传递来的URL只有http://开头的数据,所以上一个编写此程序的人在定义产品ID和出发口岸时加了这样的判断 然后...悲剧(傻逼)开始了 因为业务需求,埋点 ...

  3. 通过SSH服务登陆linux服务器(版本RHEL7)

    通过SSH服务登陆linux服务器(版本RHEL7) SSH服务概述:是一种能够以安全的方式提供远程登陆的协议,也是目前远程管理linux系统的首选方式.在此之前,我们一般使用FTP或者telnet来 ...

  4. 数据结构与算法之有序数组(2)——in dart

    本文比第一篇,采用了类实现.增加了运算符重载等功能.本来有序数组是不能修改某个位置的值的,因为这样会打破数组的有序性:但为了演示,保留了修改的方法,但为此增加了排序. import 'dart:mat ...

  5. uni-app 下拉至指定高度固定view

    uni.createSelectorQuery().select(‘#salyt’).boundingClientRect(function(rects){ console.log(rects) va ...

  6. Asp.Net实现在线人数统计 (转)

    原文件:http://blog.csdn.net/wxd_860825/article/details/4589292 利用Application对象和Session对象可以统计当前在线用户数量. 注 ...

  7. 第八周课上额外项目:pwd的实现

    项目要求: 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd 并且上交博客链接. 实验步骤 我首先不懂pwd到底是个 ...

  8. noone is not in the sudoers file ubuntu

      Login as root or su to get root prompt type visudo an editor will open find a line says root ALL=( ...

  9. PostgreSQL的 synchronous_standby_names 参数学习

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@gm ...

  10. Luogu2183【国家集训队】礼物

    题面 题解 易得答案为 $$ \sum_{i=1}^m\binom{n-\sum_{j=1}^{i-1}w_j}{\sum_{j=1}^iw_j} $$ 扩展$\text{Lucas}$即可 代码 # ...