一道来自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. 【POJ 3335】 Rotating Scoreboard (多边形的核- - 半平面交应用)

    Rotating Scoreboard Description This year, ACM/ICPC World finals will be held in a hall in form of a ...

  2. codeforces #313 div1 C

    同BZOJ 3782 上学路线 QAQ 还比那个简单一点 把坐标(1,1)-(n,m)平移成(0,0)-(n-1,m-1) 设dp[i]表示从(1,1)出发第一次经过障碍且到达第i个障碍的方案数 首先 ...

  3. mac 用 brew

    mac 下用   brew 安装插件, 有重复的不会再次安装,比xmap模式好

  4. Servlet课程0424(二) 通过继承GenericServlet来开发Servlet

    //这是第二种开发servlet的方法(继承GernericServlet) package com.tsinghua; import javax.servlet.GenericServlet; im ...

  5. 一个简单的Android小实例

    原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台   3.安装J ...

  6. 如何修改linux系统主机名称

    完成目标: 修改centos 7系统的主机名称 使用命令: hostnamectl [root@ossec-server ~]# hostnamectl --help hostnamectl [OPT ...

  7. 登陆shell与交互式非登陆shell的区别

    登录shell 所谓登录shell,指的是当用户登录系统时所取的那个shell,登录shell属于交互式shell. 登录shell将查找4个不同的启动文件来处理其中的命令. bash shell处理 ...

  8. 【HDOJ】1914 The Stable Marriage Problem

    稳定婚姻问题,Gale-Shapley算法可解. /* 1914 */ #include <iostream> #include <sstream> #include < ...

  9. 【HDOJ】4393 Throw nails

    水题,优先级队列. /* 4393 */ #include <iostream> #include <sstream> #include <string> #inc ...

  10. php 对象的执行

    1.BNF范式 %token T_OBJECT_OPERATOR "-> (T_OBJECT_OPERATOR)" unticked_statement: | expr TS ...