algorithm@ lower_bound implementation(Binary Search)
一道来自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)的更多相关文章
- [Algorithm] Beating the Binary Search algorithm – Interpolation Search, Galloping Search
From: http://blog.jobbole.com/73517/ 二分检索是查找有序数组最简单然而最有效的算法之一.现在的问题是,更复杂的算法能不能做的更好?我们先看一下其他方法. 有些情况下 ...
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- 【437】Binary search algorithm,二分搜索算法
Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...
- js binary search algorithm
js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...
- 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 ...
- 2 - Binary Search & LogN Algorithm
254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- Unity3D接入移动MM支付SDK(强联网)的问题
原地址:http://blog.csdn.net/lihandsome/article/details/11919113 因为移动MM支付的SDK只提供android版本的,要自己写过一个androi ...
- Howto: Deploy VC2008 apps without installing vcredist_x86.exe
There are several reasons for xcopy deployment of an application (also known as application local). ...
- POJ3034+DP
题意:给定一个N*N的矩阵, 然后在这个矩阵的每个格子在任意时间会出现一个鼹鼠,这个出现 出现鼹鼠的时间是输出信息所确定的. 现在你手里有一把锤子能够去锤这些鼹鼠. 你能 够移动锤子, ...
- html10个特效(转载)
http://www.html5tricks.com/10-html5-jquery-image-animatin.html 现在网页上的图片已经不再是10几年前那种低像素的静态图片了,有了HTML5 ...
- http://blog.csdn.net/xyang81/article/details/7292380
http://blog.csdn.net/xyang81/article/details/7292380
- centos6.5下Zabbix系列之Zabbix安装搭建及汉化
最近在研究zabbix,在整理完成之后就有了写一下总结博客的想法,在我研究zabbix的时候给我很大帮助的是it你好,博客地址http://itnihao.blog.51cto.com/他做的zabb ...
- ANDROID_MARS学习笔记_S01_005CheckBox
一. 1.checkbox_layout.xml <?xml version="1.0" encoding="utf-8"?> <Linear ...
- 退出myeclipse 8.5配置中心
用myeclipse 8.5没多久,进入软件中心下载插件,找不到退出按钮,唉,木想到就是一图标.
- python学习笔记二--列表
一.列表: 1. 任意类型对象的位置相关的有序集合. 2. 没有固定大小. 3. 对偏移量进行赋值及各种方法的调用,修改列表. 4. 列表是序列的一种. 5. 所有对字符串的序列操作对列表均适用. 二 ...
- The Material Sourcing Process Failed To Create Picking Suggestions in INVTOTRX (文档 ID 2003806.1)
In this Document Symptoms Cause Solution References Applies to: Oracle Inventory Management - Versio ...