[Lintcode] Maximum Gap Problem

问题描述

在一个无序的数组中,如果对其进行排序,然后扫描一遍有序数组,可以获得相邻两元素的最大差值,比如 {-1, 2, 4, 9},那么最大差值就是4和9之间,是5.

现在如果不对原始数组进行排序,有什么好的方案,来获取有序形式下的最大差值?

Given an unsorted array, find the maximum difference between the successive elements in its sorted form.

Return 0 if the array contains less than 2 elements.

问题求解

此问题还是基于桶排序的概念。可以申请一系列桶,每个桶装下某一个子区间的元素。

假设所有元素中最大值是maxNum, 最小是minNum,元素个数是n,那么平均gap就是 (maxNum-minNum)/n,如果gap是小数,则上取整。那么每个桶应该就负责长度为gap的子区间。 总的桶的数量也是 O(n)。

具体代码如下:

class Solution {
public:
    /**
     * @param nums: a vector of integers
     * @return: the maximum difference
     */
    int maximumGap(vector<int> nums) {
        int n = nums.size();
        if (n < 2) return 0;
        int maxNum = nums[0], minNum = nums[1];
        for (int i = 0; i < n; i++) {
            maxNum = max(maxNum, nums[i]); minNum = min(minNum, nums[i]);
        }
        if (maxNum == minNum) return 0;
        int gap = ceil((maxNum-minNum)*1.0/(n-1));
        int bucket_size = (maxNum - minNum) / gap + 1;
        vector<vector<int> > buckets(bucket_size);
        for (int i = 0; i < n; i++) {
            int idx = (nums[i] - minNum) / gap;
            if (buckets[idx].size() == 0) {
                buckets[idx].push_back(nums[i]); buckets[idx].push_back(nums[i]);
            } else {
                buckets[idx][0] = min(buckets[idx][0], nums[i]); buckets[idx][1] = max(buckets[idx][1], nums[i]);
            }
        }
        //for (int i = 0; i < buckets.size(); i++) { for (int j = 0; j < buckets[i].size(); j++) printf("%d ", buckets[i][j]); printf("#\n"); }
        int ans = 0, pre = -1;
        for (int i = 0; i < buckets.size(); i++) {
            if (buckets[i].size() == 0) continue;
            ans = max(ans, buckets[i][1] - buckets[i][0]);
            if (pre != -1) ans = max(ans, buckets[i][0] - buckets[pre][1]);
            pre = i;
        }
        return ans;
    }
};
时间: 2024-08-04 04:27:39

[Lintcode] Maximum Gap Problem的相关文章

[LintCode] Maximum Gap 求最大间距

Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Return 0 if the array contains less than 2 elements. Notice You may assume all elements in the array are non-negative integers and fit in the 32-

leetcode 155: Maximum Gap

Maximum Gap Total Accepted: 2946 Total Submissions: 12695 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elemen

Geeks Ford-Fulkerson Algorithm for Maximum Flow Problem 最大网络流问题

很久之前就想攻克一下网络流的问题了,一直拖着,一是觉得这部分的内容好像非常高级,二是还有很多其他算法也需要学习,三是觉得先补补相关算法会好点 不过其实这虽然是图论比较高级的内容,但是基础打好了,那么还是不会太难的,而且它的相关算法并不多,熟悉图论之后就可以学习了,就算法不会二分图也可以学习. 这里使用Ford-Fulkerson算法,其实现的方法叫做:Edmonds-Karp Algorithm 其实两者前者是基本算法思想,后者是具体实现方法. 两个图十分清楚: 图1:原图,求这个图的最大网络流

最大团问题(Maximum Clique Problem, MCP)

概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent Set Problem).目前,求解MCP问题的算法主要分为两类:确定性算法和启发式算法.确定性算法有回溯法.分支限界法等,启发式算法.蚁群算法.顺序贪婪算法.DLS-MC算法和智能搜索算法等. 问题描述: 给定无向图G=(V,E),其中V是顶点集:E是V边集.如果U属于V,且对任意两个顶点u,v

164. Maximum Gap

/* * 164. Maximum Gap * 2016-6-4 by Mingyang * 这个题目首先要求的是linear的时间,所以我个人的预测就是bucketsort * bucketsort就是把一个list分成几个bucket再分别把每一个桶排序,再合起来 * 比如我现在有10个如果selection sort就是100的时间复杂度,那么需要分成两个5 * 就是两个25相加,就是50 * 假设有N个元素A到B. * 那么最大差值不会小于ceiling[(B - A) / (N - 1

LeetCode – Refresh – Maximum Gap

Sorting solution O(nlogn): 1 class Solution { 2 public: 3 int maximumGap(vector<int> &num) { 4 int len = num.size(), result = 0; 5 if (len < 2) return 0; 6 sort(num.begin(), num.end()); 7 for (int i = 0; i < len-1; i++){ 8 result = max(res

Lintcode - Maximum Subarray III

Given an array of integers and a number k, find k non-overlappingsubarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. http://www.lintcode.com/en/problem/maximum-subarray-iii/ Thought: 一开始以为这和

【leetcode 桶排序】Maximum Gap

1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the array are non-

No.164 Maximum Gap

No.164 Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in its sorted form. Try to solve it in linear time/space. Return 0 if the array contains less than 2 elements. You may assume all elements in the