LintCode 387: Smallest Difference

题目描述

给定两个整数数组(第一个是数组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

Mon Feb 27 2017

思路

先将两个数组排序,然后分别用一个指针i, j,从前往后遍历,若A[i] > B[j],要想差更小,那么需要j++,其它情况同理。

时间复杂度是 \(O(nlogn)\),主要是需要排序。

本题还有其它的方法,遍历A数组,然后用二分查找B数组,也值得一试。

代码

// 最小差
int smallestDifference(vector<int> &A, vector<int> &B)
{
sort(A.begin(), A.end());
sort(B.begin(), B.end());
int i = 0, j = 0, min = INT_MAX;
while(i < A.size() && j < B.size())
{
int diff;
if (A[i] > B[j])
{
diff = A[i] - B[j];
++j;
}
else if (A[i] < B[j])
{
diff = B[j] - A[i];
++i;
}
else return 0;
if (diff < min) min = diff;
}
return min;
}

LintCode 387: Smallest Difference的更多相关文章

  1. LintCode "The Smallest Difference"

    Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...

  2. Smallest Difference(POJ 2718)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6740   Accepted: 18 ...

  3. POJ 2718 Smallest Difference(最小差)

     Smallest Difference(最小差) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given a numb ...

  4. The Smallest Difference

    Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...

  5. Smallest Difference(暴力全排列)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10387   Accepted: 2 ...

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

    Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...

  7. 【POJ - 2718】Smallest Difference(搜索 )

    -->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...

  8. poj 2718 Smallest Difference(暴力搜索+STL+DFS)

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6493   Accepted: 17 ...

  9. POJ 2718 Smallest Difference dfs枚举两个数差最小

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19528   Accepted: 5 ...

随机推荐

  1. 使用mdadm创建磁盘RAID10整列,RAID5出现故障,自动替换硬盘

    首先需了解mdadm的参数使用 . 第一步: 先在虚拟机中添加四块硬板 第二步:使用mdadm命令创建RAID10名称为"/dev/md0" -C代表创建操作,v 显示创建过程,- ...

  2. dstat 监控时,无颜色显示

    linux:Centos 6.6 dstat:0.7.0 注意,有这个提醒:Color support is disabled, python-curses is not installed good ...

  3. elasticsearch6 学习之安装

    安装环境:centos6.5  64位      jdk1.8      elasticsearch6.1.1 一.启动 [root@localhost bin]# ./elasticsearch - ...

  4. 传说中的WCF:消息拦截与篡改

    我们知道,在WCF中,客户端对服务操作方法的每一次调用,都可以被看作是一条消息,而且,可能我们还会有一个疑问:如何知道客户端与服务器通讯过程中,期间发送和接收的SOAP是什么样子.当然,也有人是通过借 ...

  5. vue自动路由-单页面项目(非build时构建)

    博客中自动路由的原理? 答:简单点说,就是在请求页面时,根据url进行动态添加路由. 与其它自动路由博客的区别? 目前网上的博客,一般都是在build的时候进行动态路由添加,而本博客,采用的是在获得u ...

  6. MT【129】常数变易法

    已知数列\(\{x_n\}\)满足\[x_{n+1}=\left(\dfrac 2{n^2}+\dfrac 3n+1\right)x_n+n+1,n\in\mathbf N^*,\]且\(x_1=3\ ...

  7. 【题解】CF#896 D-Nephren Runs a Cinema

    容易发现这些 vip 用户并没什么用,所以考虑枚举手持50元与100元的人共有多少个.设手持50元的人 \(a\) 个,手持100元的人 \(a - k\) 个,那么一共是 \(2*a - k\) 个 ...

  8. attention、self-attention、transformer和bert模型基本原理简述笔记

    attention 以google神经机器翻译(NMT)为例 无attention: encoder-decoder在无attention机制时,由encoder将输入序列转化为最后一层输出state ...

  9. 【模板】ISAP最大流

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  10. Libssh认证绕过CVE-2018-10933漏洞复现

    0x00 漏洞描述 libssh 0.6 及以上的版本,在服务端的代码实现中存在身份认证绕过漏洞.在向服务端认证的流程中,攻击者通过将 SSH2_MSG_USERAUTH_REQUEST 消息替换为  ...