最小差

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

解题

暴力91%数据时候超时

public class Solution {
/**
* @param A, B: Two integer arrays.
* @return: Their smallest difference.
*/
public int smallestDifference(int[] A, int[] B) {
// write your code here
if(A == null || B == null)
return -1;
int min = Integer.MAX_VALUE;
for(int i = 0;i< A.length;i++){
for(int j = 0;j<B.length;j++){
int sub = Math.abs(A[i] - B[j]);
if(min > sub)
min = sub;
}
}
return min;
}
}

先对数组排序后再求差

对排序后的两个数组,差的绝对值最小是0 ,当是 0 的时候可以直接返回答案

对于其他情况,看上面的公式,同时数组也是排序过的

ai > bj 我们要想减小差的绝对值 必须增加bi的值 j++

ai < bj 我们要想减小差的绝对值 必须增加ai的值 i++

为什么不是减小对于的 i 或j的值 这是因为我们充小开始遍历的

同时我们只遍历两个数组的最小长度,前面部分是两个数组差别最小的,或者只需看短的数组最后一个比较结果就好了。

public class Solution {
/**
* @param A, B: Two integer arrays.
* @return: Their smallest difference.
*/
public int smallestDifference(int[] A, int[] B) {
// write your code here
if(A == null || B == null)
return -1;
int min = Integer.MAX_VALUE;
Arrays.sort(A);
Arrays.sort(B);
int i = 0;
int j = 0;
while(i< A.length && j<B.length){
int sub = Math.abs(A[i] - B[j]);
min = Math.min(min,sub);
if(A[i] == B[j]){
return 0;
}else if(A[i] >B[j]){
j++;
}else if(A[i] < B[j]){
i++;
}
}
return min;
}
}

Python

class Solution:
# @param A, B: Two lists of integer
# @return: An integer
def smallestDifference(self, A, B):
# write your code here
if A == None or B == None:
return 0
MIN = abs(A[0] - B[0])
i = 0
j = 0
A.sort()
B.sort()
while i < len(A) and j<len(B):
sub = abs(A[i] - B[j])
MIN = min(MIN,sub)
if MIN == 0:
return MIN
elif A[i] >B[j]:
j+=1
else:
i+=1
return MIN

lintcode:最小差的更多相关文章

  1. [lintcode the-smallest-difference]最小差(python)

    题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 ...

  2. LintCode 387: Smallest Difference

    LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...

  3. lintcode-387-最小差

    387-最小差 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回 ...

  4. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  5. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  6. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  7. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  8. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  9. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

随机推荐

  1. quartz 数据表字典

    首次整理,可能有错误,还有少许的未整理,希望看到的人能给点补充(包括指点错误) 表名 表说明 自定义触发器 QRTZ_BLOB_TRIGGERS 列名(英) 列名(中) 数据类型 列长度 是否为空 列 ...

  2. 二、verilogHDL行为描述建模

    1.综合器: 能把行为级的verilog模块自动转换为门级结构的工具叫做综合器(synthsis tool) 2.verilog网表(verilog netlist): 电路结构可以用门级verilo ...

  3. 44.do文件格式

    style1: transcript onif {[file exists rtl_work]} {<span style="white-space:pre"> < ...

  4. nodejs笔记二--文件I/O;

    一.写入文件: fs.writeFile(filename, data, callback),数据参数可以是string或者是Buffer,编码格式参数可选,默认为"utf8",回 ...

  5. python操作sqlite数据库

    root@cacti:~/box# cat convert.py #!/usr/bin/env python import sqlite3,time,rrdtool,os def boxstatus( ...

  6. android 开发 system/app目录下面有多个重复包名的apk,会不会冲突

    环境:已经拥有了root权限的android系统,我们的apk是开机启动 测试:将2个相同的版本拷贝到系统system/app目录下面 adb root #获取root权限,前提是已经开放了root权 ...

  7. 2.C#基础篇-->数据类型

    C#数据类型分为:值类型,引用类型和指针类型(仅在不安全代码中使用) 1.值类型. 值类型包含:简单类型(整型,浮点类型和小数类型),枚举类型和结构类型.所有值类型都隐含的声明一个公共的无参构造函数, ...

  8. UVA 10002 Center of Masses

    题目链接:http://acm.uva.es/local/online_judge/search_uva.html Problem:Find out the center of masses of a ...

  9. 【BZOJ】【3439】Kpm的MC密码

    Trie树/可持久化线段树 神题啊……搞了我一下午= =(其实第233个提交也是我的) 我一开始的思路:这个找kpm串的过程,其实就跟在AC自动机上沿fail倒着往下走是差不多的(看当前是哪些点的后缀 ...

  10. Poj 1050 分类: Translation Mode 2014-04-04 09:31 103人阅读 评论(0) 收藏

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39058   Accepted: 20629 Desc ...