最小差

给定两个整数数组(第一个是数组 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. DataGridView 中CheckBox 获取状态

    /// <summary> /// /// </summary> /// <param name="sender"></param> ...

  2. vhdl基础---分频

    偶数分频 ibrary IEEE; use IEEE.STD_LOGIC_1164.ALL; use ieee.std_logic_arith; use ieee.std_logic_unsigned ...

  3. 微软职位内部推荐-Sr SDE

    微软近期Open的职位: MSN reaches nearly half a billion people across the globe where we are the #1 portal in ...

  4. 课堂练习:给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数。

    题目 1 给定一个十进制的正整数,写下从1开始,到N的所有整数,然后数一下其中出现“1”的个数. 2 要求: (1) 写一个函数 f(N) ,返回1 到 N 之间出现的“1”的个数.例如 f(12)  ...

  5. 【工具】openwrt安装记录

    步骤: 1  从以太网引导启动.由于我们实验室在服务器上放了一个openwrt镜像,安装时先从以太网启动,将服务器上的镜像载入到RAM中启动系统. 2  用SCP将在PC上编译好的openwrt-XX ...

  6. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  7. bzoj 3293 数学整理

    和1045一模一样,找到这道题的时候还愣了下神,最后发现样例都是 一样的,直接粘了1045的代码,具体题解看 http://www.cnblogs.com/BLADEVIL/p/3468729.htm ...

  8. 剑指offer--7题

    *题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变. *句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理. *例如输入“I am a student.”,则输出“st ...

  9. IE8下兼容rgba颜色的半透明背景

    在工作中做一个图片半透明遮罩时发现在IE8下不兼容 一查再知道IE8不支持rgba颜色,再搜搜兼容性方法,没想到这么快就解决了. 先说说rgba的含义: r代表red,g代表green,b代表blue ...

  10. datepicker,结束时间必须大于开始时间

    $('#js-start-time').datepicker({ dateFormat:'yy-mm-dd', onSelect: function( startDate ) { var $start ...