lintcode:最小差
最小差
给定两个整数数组(第一个是数组 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:最小差的更多相关文章
- [lintcode the-smallest-difference]最小差(python)
题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 ...
- LintCode 387: Smallest Difference
LintCode 387: Smallest Difference 题目描述 给定两个整数数组(第一个是数组A,第二个是数组B),在数组A中取A[i],数组B中取B[j],A[i]和B[j]两者的差越 ...
- lintcode-387-最小差
387-最小差 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回 ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
随机推荐
- Qt---在QLabel上实现系统时间
参考:http://blog.csdn.net/g457499940/article/details/11923887 ---------------------------------------- ...
- 为SM30视图分配事务代码
Tcode:SE93
- 在EDK里面添加ISE IP core的方法
(1)在ISE下,使用core generator,可以得到xilinx的IP的*.v和*.ngc 文件,将这两个文件拷贝出来: (2)在EDK下使用“Create or Import Periphe ...
- mysql 慢查询开启
相关博客: linux下开启mysql慢查询,分析查询语句 开启方法: 方法一:使用命令开启慢查询开启 mysql> show variables like "%long%" ...
- .net 常用正则表达式
Net中正则表达式的简单使用方法及常见验证判断 判断字符串是只是数字我们可以这样写:return new System.Text.RegularExpressions.Regex(@"^([ ...
- Why Every Professional Should Consider Blogging
转自http://www.pixelstech.net/article/1327829407-Why-Every-Professional-Should-Consider-Blogging ften ...
- TCP 粘包/拆包问题
简介 TCP 是一个’流’协议,所谓流,就是没有界限的一串数据. 大家可以想想河里的流水,是连成一片的.期间并没有分界线, TCP 底层并不了解上层业务数据的具体含义 ,它会根据 TCP 缓冲区 ...
- 第六周博客技术发表 C语言代码
#include <stdio.h> /*使用printf要包含的头文件*/#include <conio.h>void main(void) /*主 ...
- Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 离线+分块
题目链接: http://codeforces.com/contest/103/problem/D D. Time to Raid Cowavans time limit per test:4 sec ...
- Javascript使用function创建类的两种方法
1.使用function类 //myFunction.js var CMyFunc=function() { //类的公共方法,供外部调用 this.Func1=function() { var i= ...