[leetcode]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 array are non-negative integers and fit in the 32-bit signed integer range.

代码:

int maximumGap(vector<int> &num) {  //C++
        if( num.size() <2)
            return 0;

        map<int,int> myMap;
        for(int i=0; i<num.size(); i++)
            myMap.insert(make_pair(num[i],i));

        int pos =1,max=0,dif,temp;
        map<int,int>::iterator iter=myMap.begin();
        temp = iter->first;
        iter++;
        for(; iter!=myMap.end(); iter++)
        {
            dif = abs(iter->first-temp);
            if(dif >max)
                max = dif;
            temp = iter->first;

        }
        return max;
    }
时间: 2024-10-17 00:23:10

[leetcode]Maximum Gap的相关文章

[LeetCode] 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 array are non-negat

[LeetCode] 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 array are non-negat

leetcode&mdash;&mdash;Maximum Gap

    想了一晚上没想明白,上网搜了别人的答案...研究了好几个晚上才觉得有点明悟了... 下面是详细思考的过程:(参考答案) class Solution { public: // 用桶排序 // 算出相邻两个桶之间的最大差值 // 如果是平均分布,则桶的数目和元素的数目相同时,其排序的时间复杂度是0(n) // 我们假设桶的个数和元素的数目相同,若是平均分布,则每个桶里有一个数,而若某个桶里有两个以上的桶时,这时必有至少一个是空桶,那么最大间隔可能就落在空桶的相邻两个桶存储的数之间,最大间隔

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

【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-

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

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

LeetCode——Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. 原题链接: https://oj.leetcode.com/p

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