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. tomcat介绍

    Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由java语言编写,需要运行在jvm虚拟机中.之所以Java的应用 ...

  2. 蜗牛慢慢爬 LeetCode 10. Regular Expression Matching [Difficulty: Hard]

    题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...

  3. Scrum会议

    小组名称:天天向上 项目名称:连连看 成员:王森(Master) 张金生 张政 栾骄阳 时间:2016.10.18 会议内容: 已完成的内容: 张政排除连续点击Button会自动消失的Bug,张金生收 ...

  4. php aes加密

    <?php namespace Aes; error_reporting(E_ALL); ini_set('display_errors', '1'); class Aes { /** * va ...

  5. mysql my.cnf 或my.ini配置文件参数解释(转):

    #*** client options 相关选项 ***# #以下选项会被MySQL客户端应用读取.注意只有MySQL附带的客户端应用程序保证可以读取这段内容.如果你想你自己的MySQL应用程序获取这 ...

  6. windows 下升级安装mysql8,与旧版本5.6共存

    应开发需求,自mysql5.7开始引入json列类型和相关函数.为了提高数据读写的访问效率因此把数据库从mysql 5.6版升级到最新发行版 mysql 8.0.11 . 特此记录下多版本升级共存的过 ...

  7. laravel中的Contracts, ServiceContainer, ServiceProvider, Facades关系

    Contracts, ServiceContainer, ServiceProvider, Facades  Contracts 合同,契约,也就是接口,定义一些规则,每个实现此接口的都要实现里面的方 ...

  8. AJAX 跨域问题 php

    原生ajax请求方式: var xhr = new XMLHttpRequest(); xhr.open("POST", "http://xxxx.com/demo/b/ ...

  9. sourcetree git合并问题

    在使用sourcetree做多功能合并(合并不提交)的时候,有时按钮是灰色的,直接点击右上角命令行模式 git merge <branch1> --no-commit 转载请注明博客出处: ...

  10. 第215天:Angular---指令

    指令(Directive) AngularJS 有一套完整的.可扩展的.用来帮助 Web 应用开发的指令集 在 DOM 编译期间,和 HTML 关联着的指令会被检测到,并且被执行 在 AngularJ ...