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. 你可能不知道的Google Chrome命令行参数

    概述:              关于Google Chrome命令行参数(英文叫Google Chrome Command line switches),是Chrome为了实现实验性功能.方便调试. ...

  2. 新手使用R的注意事项

    1.最好先设置工作目录 如: setwd(“D:/DataDig”) 注意不是”\”,是”/” 再读取数据,如: datafile = read.csv("./test.csv") ...

  3. tone mapping简介

    以下内容转自网络: tone Mapping原是摄影学中的一个术语,因为打印相片所能表现的亮度范围不足以表现现实世界中的亮度域,而如果简单的将真实世界的整个亮度域线性压缩到照片所能表现的亮度域内,则会 ...

  4. iOS企业级开发初级课程-表视图(13集)

    首先了解了表视图的组成.表视图类的构成.表视图的分类以及表视图的两个重要协议(委托协议和数据源协议),对表视图有了一个整体上的认识.接下来我们掌握了如何实现简单表视图和分节表视图,以及表视图中索引.搜 ...

  5. hiho #1310 : 岛屿 (dfs,hash)

    题目2 : 岛屿 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给你一张某一海域卫星照片,你需要统计: 1. 照片中海岛的数目 2. 照片中面积不同的海岛数目 3. 照 ...

  6. BZOJ 3365: [Usaco2004 Feb]Distance Statistics 路程统计

    Description 一棵树,统计距离不大于 \(k\) 的点对个数. Sol 点分治. 发现自己快把点分治忘干净了... 找重心使所有儿子的最大值尽量小,然后每次处理全部子树,再减去每个子树的贡献 ...

  7. BZOJ 4544: 椭圆上的整点

    Sol 数学. 跟圆上的整点一样...TA写了个积性函数的算法...以后再说吧... \(x^2+3y^2=r^2\) \(3y^2=r^2-x^2\) \(3y^2=(r-x)(r+x)\) \(y ...

  8. 28 GroupSock(NetAddress)——live555源码阅读(四)网络

    28 GroupSock(NetAddress)——live555源码阅读(四)网络 28 GroupSock(NetAddress)——live555源码阅读(四)网络 简介 1) NetAddre ...

  9. 7.6--找过点最多的直线(CC150)

    直接两个点确定一条直线.然后两两组合,再写一个看过多少个点的函数.一直更新max就行. import java.util.Arrays; public class Solution { public ...

  10. C# 毕业证书打印《四》

    数据存储,读取控件在Panel中的位置,将控件的位置保存到xml文件中. /// <summary> /// 将当前格式写入xml /// </summary> /// < ...