[LeetCode]164.Maximum Gap,maximumgapleetcode

來源:互聯網
上載者:User

[LeetCode]164.Maximum Gap,maximumgapleetcode

題目

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.

思路

利用桶排序思想:
假設有N個元素數組array,最大值為Max,最小值為Min。
那麼最大差值不會大於ceiling[(Maz - Min) / (N - 1)]。

令bucket(桶)的大小len = ceiling[(Maz - Min) / (N - 1)],則最多會有(Max - Min) / len + 1個桶
對於數組中的任意整數K,很容易通過算式index = (K - Min) / len找出其桶的位置,然後維護每一個桶的最大值和最小值。

由於同一個桶內的元素之間的差值至多為len - 1,因此最終答案不會從同一個桶中選擇。
對於每一個非空的桶p,找出下一個非空的桶q,則q.min - p.max可能就是備選答案。返回所有這些可能值中的最大值。

代碼

/*------------------------------------------------*   日期:2015-03-23*   作者:SJF0115*   題目: 164.Maximum Gap*   來源:https://leetcode.com/problems/maximum-gap/*   結果:AC*   來源:LeetCode*   部落格:------------------------------------------------------*/#include <iostream>#include <cmath>#include <climits>#include <vector>using namespace std;class Solution {public:    int maximumGap(vector<int> &num) {        if(num.empty() || num.size() < 2){            return 0;        }//if        int size = num.size();        // 統計最大值和最小值        int Min = num[0];        int Max = num[0];        for(int i = 1;i < size;++i){            if(num[i] > Max){                Max = num[i];            }//if            if(num[i] < Min){                Min = num[i];            }//if        }//for        // 最大差值        int gap = (int)ceil((double)(Max - Min)/(size - 1));        // 桶個數        int bucketNum = (int)ceil((double)(Max - Min)/gap);        // 每個桶都維護一個最大值和最小值        vector<int> bucketsMin(bucketNum, INT_MAX);        vector<int> bucketsMax(bucketNum, INT_MIN);        for(int i = 0; i < size;++i){            if(num[i] == Max || num[i] == Min){                continue;            }//if            // 位於哪個桶            int index = (num[i] - Min) / gap;            bucketsMin[index] = min(bucketsMin[index], num[i]);            bucketsMax[index] = max(bucketsMax[index], num[i]);        }//for        // 計算最大差值        int maxGap = 0;        int pre = Min;        for(int i = 0;i < bucketNum;++i){            if(bucketsMin[i] == INT_MAX || bucketsMax[i] == INT_MIN){                continue;            }//if            maxGap = max(maxGap, bucketsMin[i] - pre);            pre = bucketsMax[i];        }//for        maxGap = max(maxGap,Max - pre);        return maxGap;    }};int main() {    Solution solution;    vector<int> vec = {4,2,1,9,5,11};    cout<<solution.maximumGap(vec)<<endl;    return 0;}

已耗用時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.