题目

给定一个未经排序的数组,请找出其排序表中连续两个要素的最大间距。

如果数组中的要素少于 2 个,请返回 0.

注意事项

可以假定数组中的所有要素都是非负整数,且最大不超过 32 位整数。

样例

给定数组 [1, 9, 2, 5],其排序表为 [1, 2, 5, 9],其最大的间距是在 5 和 9 之间,= 4.

解题

排序后找出最大间隔

堆排序后找到相邻最大值

class Solution {
/**
* @param nums: an array of integers
* @return: the maximum difference
*/
public int maximumGap(int[] nums) {
// write your code here
heapSort(nums);
int res = 0;
for(int i=0;i<nums.length-1;i++){
res = Math.max(res,nums[i+1] - nums[i]);
}
return res;
} public int left(int i){
int l = i*2+1;
return l;
}
public int right(int i){
int r = i*2 +2;
return r;
}
public void heapSort(int[] A){
int n= A.length-1;
maxHeap(A,n); for(int i=n;i>=1;i--){
swap(A,0,i);
insertToHeap(A,0,i-1);
}
}
// 构建大顶堆
public void maxHeap(int[] A,int n){ for(int i=n/2;i>=0;i--){
insertToHeap(A,i,n);
}
}
// 第i位置插入的堆中,调整为大顶堆
public void insertToHeap(int[] A,int i,int n){
int l = -1;
int r = -1;
int largest = -1;
while(true){
l = left(i);
r = right(i);
// 找到最大值id
if(l<=n && A[l] > A[i]){
largest = l;
}else{
largest = i;
}
if(r<=n && A[r] > A[largest]){
largest = r;
}
if(largest!=i){ // 不等时候交换,并更新i循环进行
swap(A,largest,i);
i = largest;
}else{
break;
} }
}
public void swap(int[] A,int i,int j){
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}

利用桶排序

参考链接

用n个桶

将n个数据放入到桶内

桶内存放可以放入该桶的数据的最大值和最小值

桶的边缘定义low,high,表示放入该桶内数据的边界,n个桶 的大小d = high - low 是固定的,并且 d = (max - min)/n

这样不会出现数据都在一个桶内的情况

最大的间隔一定在相邻桶间,gap = now.low - pre.high

class Solution {
/**
* @param nums: an array of integers
* @return: the maximum difference
*/ class Bucket{
int low;
int high;
public Bucket(){
low = -1;
high = -1;
}
} public int maximumGap(int[] num) {
if(num == null || num.length < 2){
return 0;
}
// 找到最大值最小值
int max = num[0];
int min = num[0];
for(int i=1; i<num.length; i++){
max = Math.max(max, num[i]);
min = Math.min(min, num[i]);
} // initialize an array of buckets
Bucket[] buckets = new Bucket[num.length+1]; //project to (0 - n)
for(int i=0; i<buckets.length; i++){
buckets[i] = new Bucket(); // 初始化桶
} double interval = (double) num.length / (max - min);
//distribute every number to a bucket array
for(int i=0; i<num.length; i++){
int index = (int) ((num[i] - min) * interval);
// 放入桶内
if(buckets[index].low == -1){
buckets[index].low = num[i];
buckets[index].high = num[i];
}else{
buckets[index].low = Math.min(buckets[index].low, num[i]);
buckets[index].high = Math.max(buckets[index].high, num[i]);
}
} //scan buckets to find maximum gap
int result = 0;
int prev = buckets[0].high;
for(int i=1; i<buckets.length; i++){
if(buckets[i].low != -1){
result = Math.max(result, buckets[i].low-prev);
prev = buckets[i].high;
} } return result;
}
}

lintcode:最大间隔的更多相关文章

  1. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  2. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. 块级标签包含行内标签底部出现3px间隔的解决办法

    当块级标签(如div)内包含了行内标签(如img),则外层元素与内层元素底部会出现3px的间隔: 代码如下: <!doctype html> <html lang="en& ...

  5. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  6. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  7. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  8. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  9. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

随机推荐

  1. 2015年iOS测试现状

    本文由 伯乐在线 - nathanw 翻译,dopcn 校稿.未经许可,禁止转载! 英文出处:www.mokacoding.com.欢迎加入翻译小组. 几周前,我决定将将我在 mokacoding 上 ...

  2. Linux 编辑器

    vim编辑器 vi作为Unix上的一个编辑器,一直广受欢迎.之后GUN将其移植到开源世界中,经过开发人员对其进行了改善,被称为vi improved,就是现在的vim.为了方便使用,几乎所有的Linu ...

  3. 关于在android4.1.x的版本不能启动支付宝问题

    异常:Failure calling remote service 异常日志: INFO/<unknown>(<unknown>): java.security.spec.In ...

  4. MS Chart-按照数据库的最大最小时间设置X轴label.

    核心代码: Chart1.ChartAreas[0].AxisX.Interval = (Front_Max - Front_Min).Days / 2; Chart1.ChartAreas[0].A ...

  5. 【转载】FPGA静态时序分析——IO口时序

    转自:http://www.cnblogs.com/linjie-swust/archive/2012/03/01/FPGA.html 1.1  概述 在高速系统中FPGA时序约束不止包括内部时钟约束 ...

  6. 微软职位内部推荐-Senior Software Engineer

    微软近期Open的职位: Are you looking for a big challenge? Do you know why Big Data is the next frontier for ...

  7. 颜色之RGBA

    颜色之RGBA RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法: color:rgba ...

  8. 委托、匿名委托和lambda表达式

    1.委托 在.NET中,委托有点类似于C/C++中的函数指针,但与指针不同的是,委托是一种安全的类型,那么我们就以实现两个数的差为例,先声明一个成员方法: public int CompareTwoV ...

  9. 【Roman To Integer】cpp

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  10. UVA 10002 Center of Masses

    题目链接:http://acm.uva.es/local/online_judge/search_uva.html Problem:Find out the center of masses of a ...