一道来自jhu algorithm的作业题:

Given two sorted arrays A, B, give a linear time algorithm that finds two entries i,j such that|A[i]−B[j]|is minimized. Prove the correctness of your algorithm and analyze the running time.

Solution:我们可以对于每个a[i],然后再B数组中二分查找距离a[i] “最近”的数,这里的“最近”是指|A[i]−B[j]|最小。

贴上一个algo代码 和 一个简单test案例。

package abc.com;

import java.util.Collections;

public class TestLowerBound {

    public static void prt(Object o) {
System.out.print(o);
} //return a index where (key <= a[index])
public static int lowerBound(int[] a, int key) {
int l = 0, r = a.length-1, mid;
if(key > a[r]) return -1; while(l < r) {
mid = (l+r)/2;
if(a[mid] < key) l = mid + 1;
else r = mid;
} return l;
} public static int min_abs_dif(int[] a, int val) {
int l = 0, r = a.length-1, mid; if(val <= a[l]) {
return Math.abs(val - a[l]);
}
else if(val >= a[r]) {
return Math.abs(val - a[r]);
}
else {
int pos = lowerBound(a, val);
//prt(pos);
if(Math.abs(a[pos] - val) == 0) return 0;
int min = Integer.MAX_VALUE;
if(pos-1 >= 0 && Math.abs(a[pos-1] - val) < min) min = Math.abs(a[pos-1] - val);
if(pos >= 0 && (Math.abs(a[pos] - val) < min)) min = Math.abs(a[pos] - val);
return min;
} } public static void main(String[] args) {
int[] a = {1, 29, 32, 42, 61, 63, 64, 84, 88, 99}; /*
for(int i=0; i<a.length; ++i) {
a[i] = (int)(Math.random() * 100);
prt(a[i] + " ");
}*/ int min_abs = min_abs_dif(a, 450);
prt("\n" + min_abs);
} }

algorithm@ lower_bound implementation(Binary Search)的更多相关文章

  1. [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search

    From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...

  2. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  3. 【437】Binary search algorithm,二分搜索算法

    Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...

  4. js binary search algorithm

    js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...

  5. 2 - Binary Search & LogN Algorithm - Apr 18

    38. Search a 2D Matrix II https://www.lintcode.com/problem/search-a-2d-matrix-ii/description?_from=l ...

  6. 2 - Binary Search & LogN Algorithm

    254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...

  7. [Algorithm] Delete a node from Binary Search Tree

    The solution for the problem can be divided into three cases: case 1: if the delete node is leaf nod ...

  8. [Algorithm] Check if a binary tree is binary search tree or not

    What is Binary Search Tree (BST) A binary tree in which for each node, value of all the nodes in lef ...

  9. [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search

    Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...

随机推荐

  1. uva 437 hdu 1069

    dp  将石块按三个面存入队列  按底面积排序  dp就最大高度  按嵌套矩形最长路做做法 #include <iostream> #include <cstdio> #inc ...

  2. case class inheritance

    Scala 禁止case class inheritance case class Person(name: String, age: Int) case class FootballPlayer(n ...

  3. poj 2888 Magic Bracelet

    经典的有限制条件的Burnside计数+矩阵乘法!!! 对于这种限制条件的情况我们可以通过矩阵连乘得到,先初始化矩阵array[i][j]为1.如果颜色a和颜色b不能涂在相邻的珠子, 那么array[ ...

  4. c缺陷与陷阱笔记-第三章 语义陷阱

    1.关于数组和数组指针 数组的名字默认是常量指针,值不能改变的,例如 int a[]={1,2,3,...},这个a的类型时int *,所以如果有int *p,那么a=p是合法的,其他的指针类型,例如 ...

  5. 非常好的Demo网站

    http://www.xdemo.org/

  6. 在自己的网站上实现QQ授权登录

    最近在实现QQ授权登录,现将我的实现过程以及我的理解整理如下.以下所述如有不对之处,请指正. 官方提供的SDK有:JS,PHP,Java.我的网站使用Scala+Play搭建的,所以只能用JS SDk ...

  7. QxtFlowView(libqxt)

    https://github.com/mnutt/libqxt/tree/master/examples http://libqxt.bitbucket.org/doc/0.5/class_qxt_f ...

  8. 【转】Android Service被关闭后自动重启,解决被异常kill 服务

    http://www.kaifajie.cn/android/10182-2.html 每次调用startService(Intent)的时候,都会调用该Service对象的onStartComman ...

  9. hdu 1412 (STL list)

    简单例题 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1412 list 相关博客:http://www.cnblogs.com/fangyukuan/a ...

  10. Compass 编译.scss文件的问题

    compass 命令编译scss文件存在一个问题: 不能对"_"下划线开头的scss文件名称的文件进行编译.将"_"去掉就可以啦