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 ...
随机推荐
- Swift 中的利刃,函数和闭包
input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...
- scjp考试准备 - 6 - 父类构造器的引用
题一,如下代码的执行结果: class Person{ String name = "No name"; public Person(String nm){name = nm;} ...
- 通过store为toolbar添加按钮
目的是实现导航条toolbar可以动态加载按钮. ExtJS的版本是4.0. 实现方案有两个.方案一:添加render事件监听,在监听事件处理函数中使用Ext.Ajax获取按钮信息,实现动态添加按钮. ...
- JNI文件中命名类与JAVA文件中匹配
jni.c中注册中 int register_android_boa(JNIEnv *env){ jclass clazz; static const char* const kClass ...
- UIView 添加子视图的常用方法
1. - (void)addSubview:(UIView *)view 这是最常用的方法有两个注意点 参数view可以是nil,运行不会报错,当然,父视图的subViews也不会增加. 此方法增加 ...
- 使用C语言在Win控制台中输出带颜色的文字
学了这么久的C语言,一直停留在编写“控制台”程序的水平.黑色窗口,白色的文字,看多了着实让人感到枯燥无味.但是作为业余爱好者,我既没有那么多时间和精力去学习如何编写窗口程序,也没有那个必要一定用C去调 ...
- @synthesize 有什么好处?
如果不用 synthesize,操作的是 @property中定义的变量,使用synthesize之后,间接的操作了一个新的成员变量,到底有什么好处?直接只用一个@property不是更简单吗?
- win8.1 cygwin编译java轻量虚拟机avian
1.背景 昨天在网上看到别人用aauto写本地小程序写的很爽,我觉得如果java的jre能小一点,凭借java庞大的第三方类库写小工具也还算不错的.本人就经常用eclipse+一些commons包写些 ...
- hibernate tool连接oracle生成pojo和xml文件无法查询表解决办法
需要在hibernate的配置文件中增加 <property name="hibernate.default_schema">[username]</proper ...
- 802.11 wireless 1(主要还是学习ccna wireless的体系)
802.11 wireless 1(主要还是学习ccna wireless的体系)ISM频带(ISM band starts early 1990s)900MHZ 2.4GHZ 5GHZ 四种 ...