最小差

给定两个整数数组(第一个是数组 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. 自定义debug信息

    #ifdef  DEBUG  #define debug(fmt,args...)  printk(fmt ,##args)  #define debugX(level,fmt,args...) if ...

  2. UIStepper swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  3. 微软职位内部推荐-Senior Software Engineer-News

    微软近期Open的职位: News is a critical areas for integration of mobile and services, one of the top priorit ...

  4. jta.properties transactions.properties Log already in use 解决方法

    当在resin里跑多个含有atomikos控制事物的项目时,会报错,Log already in use. 解决方法: 加jta.properties或者transactions.properties ...

  5. Java程序员面试中的多线程问题

    很多核心Java面试题来源于多线程(Multi-Threading)和集合框架(Collections Framework),理解核心线程概念时,娴熟的实际经验是必需的.这篇文章收集了Java线程方面 ...

  6. ubuntu cron.hourly不运行

    服务器没有装NTP,要每天向特定的server进行时间同步,写了一个定时任务,放在/etc/cron.daily下,但是不运行. /etc/crontab文件: # /etc/crontab: sys ...

  7. LabView调用C#混合模式dll

    在一些特定要求下,我们的C#可能需要制作dll给LabView进行调用,并且我们不能够保证C#的程序是完全自己写而不调用第三方的dll库.很多时候我们需要使用诸如Sqlite.Net.AForge.N ...

  8. 【WildCard Matching】cpp

    题目: Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single charact ...

  9. 重读《Struts In Action》

    Figure   1.1. The Java Servlet API exposes the HTTP client/server protocol to the Java   platform. S ...

  10. shell ulimit -n

    通过ulimit -n命令可以查看linux系统里打开文件描述符的最大值,一般缺省值是1024,