题目

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.

分析

题意为,给定一组无序整数序列,求其有序形式下相邻元素的最大差值;

要求,时空复杂度均为线性。

开始还以为题意理解错了,尝试做了一下,利用库排序函数sort排序,然后一次遍历求得最大差值;时间复杂度为O(nlogn),空间复杂度为O(1);没想到AC了;

看来,题目要求如上描述很简单,考察的关键在于能否实现线性的优化也就延伸为排序算法的线性优化;

参考了别人的桶排序实现代码 ^||^… 羞愧的附上~~~

AC代码

class Solution {
public:
//方法一:STL库函数Sort排序,T(n)=O(nlogn)
int maximumGap1(vector<int>& nums) {
if (nums.empty() || nums.size() < 2)
return 0; int len = nums.size();
sort(nums.begin(), nums.end());
int gap = 0;
for (int i = 1; i < len; ++i)
{
if (nums[i] - nums[i - 1] > gap)
gap = nums[i] - nums[i - 1];
}//for
return gap;
} //方法二:桶排序
int maximumGap(vector<int>& nums) {
if (nums.empty() || nums.size() < 2)
return 0;
int n = nums.size();
int minAll = *min_element(nums.begin(), nums.end());
int maxAll = *max_element(nums.begin(), nums.end());
// type conversion!!!
double gap = ((double)(maxAll - minAll)) / (n - 1);
// compute min and max element for each bucket
vector<int> minV(n - 1, INT_MAX);
vector<int> maxV(n - 1, INT_MIN);
for (int i = 0; i < n; i++)
{
if (nums[i] != maxAll)
{// the bktId of maxAll will fall out of bucket range
int bktId = (int)((nums[i] - minAll) / gap);
minV[bktId] = min(minV[bktId], nums[i]);
maxV[bktId] = max(maxV[bktId], nums[i]);
}
}
int ret = 0;
int curMax = maxV[0];
for (int i = 1; i < n - 1; i++)
{
if (minV[i] != INT_MAX)
{
ret = max(ret, minV[i] - curMax);
curMax = maxV[i];
}
}
ret = max(ret, maxAll - curMax);
return ret;
}
};

GitHub测试程序源码

LeetCode(164)Maximum Gap的更多相关文章

  1. LeetCode(152) Maximum Product Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  2. LeetCode(104) Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  3. LeetCode(53) Maximum Subarray

    题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...

  4. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  5. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  6. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  7. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  8. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  9. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

随机推荐

  1. 061 Rotate List 旋转链表

    给定一个链表,将链表向右旋转 k 个位置,其中 k 是非负数.示例:给定 1->2->3->4->5->NULL 且 k = 2,返回 4->5->1-> ...

  2. PS高级特训班 百度云资源(价值2180元)

    课程目录:   第1章第一期1第一节 火焰拳头1:12:252第二节 荷叶合成00:05:143第三节 新年巨惠海报(一)1:00:374第四节 新年巨惠海报(二)1:05:345第五节 美食印刷品1 ...

  3. 用canvas裁剪图片

    var selectObj = null; function ImageCrop(canvasId, imageSource, x, y, width, height) { var canvas = ...

  4. JAVA分包下项目部分代码存储

    一.注册时姓名去重和符合汉字格式: // 新用户申请加入 public void NewHuman() { System.out.println("========新会员申请加入页面==== ...

  5. null、undefined和NaN的区别

    未定义的值和定义未赋值的值是undefined: null是一种特殊的Object,可以给变量赋一个值null,来清除变量的值: NaN是一种特殊的number:

  6. 访问权限修饰符-static-final-this-super-匿名对象

    1.this关键字的作用     1)调用本类中的属性;     2)调用本类中的构造方法;且只能放首行,且必须留一个构造方法作为出口,即不能递归调用     3)表示当前对象; 2.匿名对象     ...

  7. Java集合框架—Set

    集合框架 Set的特点:无序,不可以重复元素. (1)HashSet:数据结构是哈希表.线程是非同步的.               保证元素唯一性的原理:判断元素的hashCode值是否相同.   ...

  8. [Oracle 视图] ALL_OBJECTS

    ALL_OBJECTS ALL_OBJECTS describes all objects accessible to the current user. ALL_OBJECTS描述当前用户的可访问的 ...

  9. 【虚拟机-网络IP】虚拟机配置静态 IP 以后无法连接的解决办法

    问题描述 将虚拟机内部 IP 地址从动态获取改成静态 IP 以后,远程连接失败. 问题分析 Azure 虚拟机的内部 IP 默认为动态分配, 由 DHCP 服务自动分配, 在虚拟机的生命周期内, 该 ...

  10. (转)!注意:PreTranslateMessage弹出框出错

    dlg.DoModal()截住了界面消息,所以返回时原来的pMsg的内容已经更改了,消息,窗口句柄都不在是if以前的值了,而且窗口句柄应该是对话框里的子窗口的句柄,所以调用CFrameWnd::Pre ...