leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题。
在一个没排序的数组里,找出排序后的相邻数字的最大差值。
要求用线性时间和空间。
如果用nlgn的话,直接排序然后判断就可以了。so easy
class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < ) return ;
sort(num.begin(), num.end());
int maxm = -;
for (int i = ; i < num.size(); i++)
{
if (abs(num[i] - num[i-]) > maxm)
maxm = abs(num[i] - num[i-]);
}
return maxm;
}
};
但我们要的是线性时间。
其实这个思想在算法课上有讲过。用桶的思想。把数组分成几个桶,然后判断相邻桶的最大与最小之间的差值。关键是要知道每个桶的长度,已经桶的个数。
class Solution {
public:
int maximumGap(vector<int> &num) {
if (num.size() < ) return ;
int Max = num[], Min = Max, ans = ;
for (int i = ; i < num.size(); i++)
{
if (num[i] < Min)
Min = num[i];
if (num[i] > Max)
Max = num[i];
}
if (Max == Min) return ;
int bucketGap = (Max - Min)/num.size() + ; // 桶的间隔是关键
int bucketLen = (Max - Min)/bucketGap + ; // 举个 1,2,3的例子就知道了
vector<int> MinMax(, INT_MAX);
MinMax[] = INT_MIN;
vector<vector<int> > bucket(bucketLen, MinMax);
for (int i = ; i < num.size(); i++)
{
int ind = (num[i] - Min)/bucketGap;
if (num[i] < bucket[ind][])
bucket[ind][] = num[i];
if (num[i] > bucket[ind][])
bucket[ind][] = num[i];
}
int first = bucket[][], second;
for (int i = ; i < bucketLen; i++)
{
if (bucket[i][] == INT_MAX) continue;
second = bucket[i][];
int tmpmax = second - first;
ans = tmpmax > ans ? tmpmax : ans;
first = bucket[i][];
}
return ans;
}
};
最后附上官网的解法说明:
Suppose there are N elements and they range from A to B.
Then the maximum gap will be no smaller than ceiling[(B - A) / (N - 1)]
Let the length of a bucket to be len = ceiling[(B - A) / (N - 1)], then we will have at most num = (B - A) / len + 1 of bucket
for any number K in the array, we can easily find out which bucket it belongs by calculating loc = (K - A) / len and therefore maintain the maximum and minimum elements in each bucket.
Since the maximum difference between elements in the same buckets will be at most len - 1, so the final answer will not be taken from two elements in the same buckets.
For each non-empty buckets p, find the next non-empty buckets q, then q.min - p.max could be the potential answer to the question. Return the maximum of all those values.
leetcode[164] Maximum Gap的更多相关文章
- LeetCode 164. Maximum Gap[翻译]
164. Maximum Gap 164. 最大间隔 Given an unsorted array, find the maximum difference between the successi ...
- [LeetCode] 164. Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- ✡ leetcode 164. Maximum Gap 寻找最大相邻数字差 --------- java
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Java for LeetCode 164 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【刷题-LeetCode】164 Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- dom 规划(html和xml)
html dom与xml dom关联: 什么是 DOM? DOM 是 W3C(万维网联盟)的标准. DOM 定义了訪问 HTML 和 XML 文档的标准: "W3C 文档对象模型 (DOM) ...
- android变化HOLO对话风格
andriod风修改对话框格,通过设置theme实现.一些要素需要通过Java代码更改,下面的对话框更改的步骤的例子称号. 1.写文本样式. DIALOG标题是textview,在sytles.xml ...
- 【Machine Learning】Mahout基于协同过滤(CF)的用户推荐
一.Mahout推荐算法简介 Mahout算法框架自带的推荐器有下面这些: l GenericUserBasedRecommender:基于用户的推荐器,用户数量少时速度快: l GenericI ...
- 应用ExcelPackage导出Excel
前阵子工作需要,要实现从数据库中导出数据到Excel.老套路 先去百度上查阅资料,发现了以下几种方法: 1:将DataGrid控件中的数据导出Excel 2:将dataview导出excel 3:从网 ...
- Java JSON处理库Jackson
Jackson是一款为Java平台提供的一套数据处理类库工具,Jackson的主要功能是提供JSON解析和生成.另外,Jackson还提供额外的类库以支持处理Avro, CBOR, CSV, Smil ...
- Xcode5和6共处,如何发布应用程序存储
怎样你和我一样手贱安装了Xcode6,同一时候又须要公布应用到商店时,你会发现打好的包是通只是审核的. 验证报错: unable to validate application archives of ...
- NYOJ353 3D dungeon 【BFS】
3D dungeon 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 You are trapped in a 3D dungeon and need to find ...
- Swift使用单个案件管理FMDB数据库
下班... 抢 我曾经Swift使用单一个案管理FMDB数据库的方法共享出来: // Created by 秦志伟 on 14-6-12. import UIKit class ZWDBManager ...
- 第6章 适配器模式(Adapter Pattern)
原文 第6章 适配器模式(Adapter Pattern) 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 解决的问 ...
- 手机新闻网站,手持移动新闻,手机报client,jQuery Mobile手机新闻网站,手机新闻网站demo,新闻阅读器开发
我们坐在地铁.经常拿出新浪手机查看新闻.腾讯新闻,或者看新闻,等刷微信功能.你有没有想过如何实现这些目标. 移动互联网.更活泼. 由于HTML5未来,jQuery Moblie未来. 今天我用jqm的 ...