美文网首页
Lintcode400 Maximum Gap solutio

Lintcode400 Maximum Gap solutio

作者: 程风破浪会有时 | 来源:发表于2018-01-12 00:01 被阅读0次

【题目描述】

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-bit signed integer range.

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

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

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

【题目链接】

www.lintcode.com/en/problem/maximum-gap/

【题目解析】

假设有N个元素A到B。

那么最大差值不会小于ceiling[(B - A) / (N - 1)]

令bucket(桶)的大小len = ceiling[(B - A) / (N - 1)],则最多会有(B - A) / len + 1个桶

对于数组中的任意整数K,很容易通过算式loc = (K - A) / len找出其桶的位置,然后维护每一个桶的最大值和最小值

由于同一个桶内的元素之间的差值至多为len - 1,因此最终答案不会从同一个桶中选择。

对于每一个非空的桶p,找出下一个非空的桶q,则q.min - p.max可能就是备选答案。返回所有这些可能值中的最大值。

【参考答案】

www.jiuzhang.com/solutions/maximum-gap/

相关文章

  • Lintcode400 Maximum Gap solutio

    【题目描述】 Given an unsorted array, find the maximum differen...

  • Maximum Gap

    https://leetcode.com/problems/maximum-gap/给定一个无序int数组,求出排...

  • 2019-01-16

    LeetCode 164. Maximum Gap Description Given an unsorted a...

  • Leetcode - Maximum Gap

    My code: 看到这道题目,第一个想法是排序。然后要求排序时间复杂度是 O(n)就想到了 radix sort...

  • LintCode 最大间距

    http://www.lintcode.com/zh-cn/problem/maximum-gap/ 最大间距 查...

  • ARTS 第20周

    ARTS 第20周分享 [TOC] Algorithm 164. Maximum Gap [hard] [题目描述...

  • 164. Maximum Gap

    问题描述 Given an unsorted array, find the maximum difference...

  • Leetcode 164. Maximum Gap

    Given an unsorted array, find the maximum difference betw...

  • LeetCode 164. Maximum Gap

    Description Given an unsorted array, find the maximum dif...

  • Leetcode-164-Maximum Gap

    给定一个未排序的数组,求排序后数组中相邻元素之差的最大值。 这题最大的思维盲点就在于的复杂度让人直接放弃包含排序的...

网友评论

      本文标题:Lintcode400 Maximum Gap solutio

      本文链接:https://www.haomeiwen.com/subject/apqkwxtx.html