zoukankan      html  css  js  c++  java
  • lintcode:最大间隔

    题目

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

    如果数组中的要素少于 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;
    }
    }
  • 相关阅读:
    非常强大的table根据表头排序,点击表头名称,对其内容排序
    处理字符集中的算式问题
    java基础复习:final,static,以及String类
    try,catch,finally含return时的执行顺序及丢失的伪例
    系统 触发器
    某个表按某个列分组且按另一个列排序
    XML读取
    硬软件交互原理
    TcpClient 读写流
    C# Socket的TCP通讯 异步 (2015-11-07 10:07:19)转载▼
  • 原文地址:https://www.cnblogs.com/bbbblog/p/5653955.html
Copyright © 2011-2022 走看看