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.

思路:

这是一道难题,难点在于不能排序,但是要找到在排序的情况下相邻数字的最大差。

各种想不出来,很low的用了排序,居然也通过了。

然后看答案,发现可以很巧妙的利用桶的思想。找到数组里面的最大值和最小值,则数字间隔gap满足:

gap >= (max - min) / (N - 1)  结果向下取整  注意gap = 0 时取 1, 防止后面除0

然后,可以分为 (max - min) / gap + 1 个桶

每个数字根据  (num[i] - min) / gap 来决定放在哪个桶里。

记录每个桶里的最大值和最小值。

则最大间隔不会在桶内,而会在相邻的桶之间,bucket[i].min - bucket[i - 1].max 中最大的数字就是目标数字。

//这个虽然通过了,但是用了排序,是O(nlogn)的算法
int maximumGap(vector<int> &num) {
int ans = ;
sort(num.begin(), num.end());
for(int i = ; i < num.size(); i++)
{
ans = num[i] - num[i - ];
}
return ans;
}
---------------------------------------------------------------------------------
//答案的思路
int maximumGap1(vector<int> &num) {
if(num.size() < ) return ;
//第一步,找最大和最小值
int maxnum = num[];
int minnum = num[];
for(int i = ; i < num.size(); i++)
{
maxnum = (maxnum < num[i]) ? num[i] : maxnum;
minnum = (minnum > num[i]) ? num[i] : minnum;
}
//第二步:判断间隔的大小
int gap = (maxnum - minnum) / (num.size() - );
gap = (gap == ) ? : gap;
//判断和记录每个桶的最大和最小数字
vector<vector<int>> bucket((maxnum - minnum) / gap + , vector<int>(, -)); //只需记录每个桶的最大和最小数字
for(int i = ; i < num.size(); i++)
{
int belong = (num[i] - minnum) / gap;
bucket[belong][] = (num[i] > bucket[belong][]) ? num[i] : bucket[belong][]; //更新最大值
bucket[belong][] = (bucket[belong][] < || num[i] < bucket[belong][]) ? num[i] : bucket[belong][]; //更新最小值
} //找最大间隔
int ans = ;
int pre = ;
for(int i = ; i < bucket.size(); i++)
{
if(bucket[i][] == -) continue;
ans = (bucket[i][] - bucket[pre][] > ans) ? bucket[i][] - bucket[pre][] : ans;
pre = i;
}
return ans;
}

网上还有一种方法是利用基数排序,我还没看。

int maximumGap(std::vector<int> &num) {
for(unsigned bit = ; bit < ; bit++)
std::stable_partition(num.begin(), num.end(), [bit](int a){
return !(a & ( << bit));
});
int difference = ;
for(std::size_t i = ; i < num.size(); i++) {
difference = std::max(difference, num[i] - num[i-]);
}
return difference;
}

【leetcode】Maximum Gap(hard)★的更多相关文章

  1. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  2. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  3. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  4. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  5. 【LeetCode】Maximum Depth of Binary Tree

    http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...

  6. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  7. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

  8. 【leetcode】Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  9. 【Leetcode】Maximum Product Subarray

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

随机推荐

  1. JS,html压缩及混淆工具

    现在已经出现了不少有自己特色的: JSMin Javascript compressor Packer Closure Compiler YUI Compressor Pretty Diff Java ...

  2. ios如何普安短图片类型

    很多时候需要知道服务器返回的图片是.png还是.jpg或者是.git, 两种方式 1,获取扩展名 //图片    NSString *image = @"4351141241.GIT&quo ...

  3. Javascript面向对象编程一:基础篇

    该随笔分为以下四部分: Javascript面向对象编程一:基础篇 Javascript面向对象编程二:封装 Javascript面向对象编程三:继承 Javascript面向对象编程四:控件 先弄个 ...

  4. Linux程序编写shell script的格式

    #!/bin/bash #program # 在此处写下此程序的作用 #History: #此处写下写此程序的时间 作者 版本号 PATH=/bin:/sbin:/usr/bin:/usr/sbin: ...

  5. C 语言sscanf

    C语言以sscanf逗号作为分割符 ]={}; ]={}; ]={}; sscanf(],&buf_b[],&buf_b[]); printf("************** ...

  6. git之remote repository create(远程仓库创建)

    参考:Git教程 - 廖雪峰的官方网站 1.在Git bash窗口执行如下指令创建SSH KEY: ssh-keygen -t rsa -C "sunjf@biomarker.com.cn& ...

  7. springmvc之hibernate整合

    1.项目结构 2.所需jar包 3.web.xml <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  8. Hello 畅连·西瓜 帮助与更新

    无感认证很好用,软件不再更新, 感谢每一位朋友的陪伴,谢谢! (2016.12.15) 百度云:点击下载 ------------旧版更新日志------------- Hello 畅连·西瓜 官网: ...

  9. CentOS 6.5 3.0.4安装agentd

    更改主机名为test3 [root@localhost ~]# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=test3 关闭selinux [ ...

  10. hexo问题篇(偶尔抽抽疯)

    hexo安安稳稳的跑了很久,然后 ....让人心碎的hexo问题,华丽丽的摔倒在坑里,只因update了hexo version最是哪一句 hexo server让人欲哭无泪 -问题场景 设备: Ma ...