一道来自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. Win32 DLL和MFC DLL 中封装对话框

    现在最常看见的关于DLL的问题就是如何在DLL中使用对话框,这是一个很普遍的关于如何在DLL中使用资源的问题.这里我们从Win32   DLL和MFC   DLL两个方面来分析并解决这个问题.     ...

  2. define中的:#,##,#@

    [define中的:#,##,#@] #define Conn(x,y) x##y #define ToChar(x) #@x #define ToString(x) #x (2)x##y表示什么?表 ...

  3. shell中截取字符串的方法总结

    shell中截取字符串的方法有很多种, ${expression}一共有9种使用方法. ${parameter:-word} ${parameter:=word} ${parameter:?word} ...

  4. Posix IPC

  5. BZOJ 3747 POI2015 Kinoman

    因为上午没有准备够题目,结果发现写完这道题没题可写了QAQ 又因为这道题范围是100w,我写了发线段树,以为要T,上午就花了一个小时拼命卡常数 结果下午一交居然过了QAQ 我们考虑枚举L,求最大R使得 ...

  6. Google Code Style

    Google开源项目的代码遵循的规范,见这,C++, OC. PS: vim的配色编辑用户主目录下的.vimrc即可.

  7. 关于haproxy hdr_reg(host) 的一些解释

    I've recently taken over an environment using HAProxy, and I'm attempting to learn the config and wh ...

  8. UVa 1213 (01背包变形) Sum of Different Primes

    题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...

  9. angularJS $resource与后台restapi的对应关系

    REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.RESTful风格的设计不仅 ...

  10. 【转】Cygwin的包管理器:apt-cyg

    原文网址:http://zengrong.net/post/1792.htm Cygwin的包管理工具setup.exe实在是难用的让人蛋碎.于是就有了这样一个apt-cyg,可以提供类似于 apt- ...