lintcode-387-最小差
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-最小差的更多相关文章
- LintCode 387: Smallest Difference
LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...
- [lintcode the-smallest-difference]最小差(python)
题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 ...
- lintcode:最小差
最小差 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
随机推荐
- thinkphp3.2+cropper上传多张图片剪切图片
实现效果截图 点加号可以继续上传第二张图片 代码部<--引入cropper相关文件--> <link rel="stylesheet" href="/h ...
- Python - 入门基础(一)
1.解释器路径 #!/usr/bin/env python 2.编码 # -*- coding:utf8 -*- 1.ascill ---00000000 (8个位表示) 缺点:表示不了英文 2.u ...
- exynos4412—链接脚本复习
在u-boot下,定义变量, 编译,编译完后 使用arm-linux-nm arm 没有去头的二进制可执行文件 都在BSS段,均为初始化. 打印之后会出算随机值. 目前还处于uboot阶段,如 ...
- 树莓派ubuntu系统下修改config.txt文件 树莓派config.txt文件修改记录
原文:https://www.raspberrypi.org/documentation/configuration/config-txt.md译文:http://my.oschina.net/fun ...
- UVA 514 - Rails ( 铁轨)
from my CSDN: https://blog.csdn.net/su_cicada/article/details/86939523 例题6-2 铁轨(Rails, ACM/ICPC CERC ...
- 手动安装R包
1,先将R包下载到本地 2,getwd() setwd("F:\\CNV\\Paper\\Case-control\\mHMM") 3, install.packages(&quo ...
- 自定义udf添加一列
//创建得分窗口字典 var dict= new mutable.HashMap[Double, Int]() ){ dict.put(result_Score(i),i) } //自定义Udf函数 ...
- django中models的filter过滤方法
__gt 大于__gte 大于等于 __lt 小于 __lte 小于等于 __in 存在于一个list范围内 __startswith 以...开头 __is ...
- I/O: std::ios_base::openmode
I/O: std::ios_base::openmode std::ios_base::openmode std::ios_base::in: 打开文件进行读操作,即读取文件中的数据 如果指定路径中 ...
- SimpleDateFormat,Calendar 线程非安全的问题
SimpleDateFormat是Java中非常常见的一个类,用来解析和格式化日期字符串.但是SimpleDateFormat在多线程的环境并不是安全的,这个是很容易犯错的部分,接下来讲一下这个问题出 ...