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. 搭建企业级Docker Registry -- Harbor

    Harbor 是一个企业级的 Docker Registry,可以实现 images 的私有存储和日志统计权限控制等功能,并支持创建多项目(Harbor 提出的概念),基于官方 Registry V2 ...

  2. CMD命令去导出文件下的文件名称到EXCEL

      dir C:\Users\caire\Pictures\壁纸/b>E:\temp.xls

  3. python & dict & switch

    python & dict & switch python 中是没用switch语句的,这应该是体现python大道至简的思想,python中一般多用字典来代替switch来实现. # ...

  4. Nagios学习笔记

    1 Nagios功能 1.1  监控工具 1.2  可以监控主机/服务或者资源 1.3  四种状态值 OK,WARNING,CRITICAL,UNKNOWN CPU:90%(CRITICAL),80% ...

  5. 【刷题】洛谷 P3573 [POI2014]RAJ-Rally

    题目描述 An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long di ...

  6. IoT与区块链的机遇与挑战

    区块链, 分布式账本技术的一种形式, 自从2014年或多或少地获得了大量的关注: 区块链和物联网, 区块链和安全, 区块链和金融, 区块链和物流, 凡是你能想到的,仿佛都可以应用区块链. 在本文中, ...

  7. SpringMVC DispatcherServlet-------视图渲染过程

    整个spring mvc的架构如下图所示: 现在来讲解DispatcherServletDispatcherServlet的最后一步:视图渲染.视图渲染的过程是在获取到ModelAndView后的过程 ...

  8. wazhu之agent manage

      代理生命周期 注册代理 一旦代理程序安装在要监控的计算机上,就必须向Wazuh管理器注册才能建立通信.这可以通过命令行,Authd或RESTful API完成. 注册代理将保留在管理器中,直到用户 ...

  9. Android中Selector的用法(改变ListView和Button的默认背景)

    Android中的Selector的用法 http://blog.csdn.net/shakespeare001/article/details/7788400#comments Android中的S ...

  10. Codeforces 901C. Bipartite Segments(思维题)

    擦..没看见简单环..已经想的七七八八了,就差一步 显然我们只要知道一个点最远可以向后扩展到第几个点是二分图,我们就可以很容易地回答每一个询问了,但是怎么求出这个呢. 没有偶数简单环,相当于只有奇数简 ...