LintCode 387: Smallest Difference
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的更多相关文章
- LintCode "The Smallest Difference"
Binary search. class Solution { int _findClosest(vector<int> &A, int v) { , e = A.size() - ...
- Smallest Difference(POJ 2718)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6740 Accepted: 18 ...
- POJ 2718 Smallest Difference(最小差)
Smallest Difference(最小差) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given a numb ...
- The Smallest Difference
Given two array of integers(the first array is array A, the second array is arrayB), now we are goin ...
- Smallest Difference(暴力全排列)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10387 Accepted: 2 ...
- POJ 2718 Smallest Difference(贪心 or next_permutation暴力枚举)
Smallest Difference Description Given a number of distinct decimal digits, you can form one integer ...
- 【POJ - 2718】Smallest Difference(搜索 )
-->Smallest Difference 直接写中文了 Descriptions: 给定若干位十进制数,你可以通过选择一个非空子集并以某种顺序构建一个数.剩余元素可以用相同规则构建第二个数. ...
- poj 2718 Smallest Difference(暴力搜索+STL+DFS)
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6493 Accepted: 17 ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
随机推荐
- JPEG标准推荐的亮度、色度DC、AC Huffman编码表
JPEG 标准推荐的亮度.色度DC.AC Huffman 编码表 博主在完成数字图像处理大作业时利用搜索引擎查找了很久完整的四张Huffman 编码表(亮度AC Huffman编码表.亮度DC Huf ...
- 『编程题全队』Alpha 阶段冲刺博客集合
『编程题全队』Alpha 阶段冲刺博客集合 »Day1 2018年4月19日 博客连接:『编程题全队』Alpha 阶段冲刺博客Day1 »Day2 2018年4月20日 博客连接:『编程题全队』Alp ...
- TP5 助手函数与TP3.2单字母函数
一.TP5 助手函数 助手函数 描述 abort 中断执行并发送HTTP状态码 action 调用控制器类的操作 cache 缓存管理 config 获取和设置配置参数 controller 实例化控 ...
- [Linux] 关于 history 命令的简单使用
1. 如果不想记录当前 会话操作的命令的话 可以使用命令 set +o history 但是这个命令有一个坏处, 他自己被记住了... 2. 还有一个办法 就是 在退出登录之前执行命令 history ...
- [转]正确设置nginx/php-fpm/apache权限
核心总结:php-fpm/apache 进程所使用的用户,不能是网站文件所有者. 凡是违背这个原则,则不符合最小权限原则. 根据生产环境不断反馈,发现不断有 php网站被挂木马,绝大部分原因是因为权限 ...
- IE下Userdata本地化存储
这两天看了下Discuz x2发帖的实时保存机制,涉及到本地化存储,所以上网查了下,Firefox等支持HTML5的浏览器使用window.localStorage或window.sessionSto ...
- vue element 新增、编辑类Dialog公用函数
调用 <el-button type="primary" class="my-button" size="small" :loadin ...
- 【bzoj2780】 Sevenk Love Oimaster
http://www.lydsy.com/JudgeOnline/problem.php?id=2780 (题目链接) 题意 给出很多主串和很多询问串,求一个询问串在多少主串中出现过 Solution ...
- Java虚拟机加载类
Java虚拟机是如何加载Java类的 Java虚拟机加载Java类总共需要经过3步:加载-----链接-----初始化.Java语言的类型可以分为两大类:基本类型和引用类型.基本类型是有Java虚拟机 ...
- 如何让自己的广播只让指定的 app 接收?
1.自己的应用(假设名称为应用 A)在发送广播的时候给自己发送的广播添加自定义权限,假设权限名为:com.itheima.android.permission , 然后需要在应用 A 的 Androi ...