【leetcode】Maximum Gap(hard)★
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)★的更多相关文章
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【LeetCode】Maximum Depth of Binary Tree
http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 【LeetCode】Maximum Subarray(最大子序和)
这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...
- 【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 ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- [译]Profile and debug your ASP.NET MVC app with Glimpse
原文:http://www.asp.net/mvc/overview/performance/profile-and-debug-your-aspnet-mvc-app-with-glimpse Gl ...
- jquery选择器(三)-过滤选择器
一.基本过滤选择器 二.内容过滤选择器 1. 包含文本内容为“text”的元素 2. 含有某个选择器所匹配的父元素 3. 包含有子元素或者文本的父元素 4. 不含有子元素或者文本的父元素 三.可见性过 ...
- C#入门随手笔记
1..Net Framework是Microsoft为开发应用程序而创建的一个开发平台. 运行操作系统不限:Microsoft版本运行在windows,Mono版本运行开Linux和MacOS: 应用 ...
- redis.1--SDS结构
1. Redis 没有直接使用c语言的字符串(以空字符结尾的字符数组),而是自己构建了一 种名为简单动态字符串(Simple Dynamic String , SDS),并将SDS做为 ...
- Windows下MySQL 5.6安装及配置详细图解
一.安装前的准备 1.下载安装程序包,可到MySQL官方网站http://www.mysql.com/下载,如图1-1: 图1-1 下载后的安装文件如图1-2所示: 图1-2 二.安装 1.双击下载的 ...
- 为在韶大痛苦而不能用手机、Pad等上网的同志造福!
目标:共享咱们校园网,让更多的人或更多的设备冲浪去! 基本条件:一台带无线功能的笔记本,一个可以上网的账号与pwd,最好为Windows7以上的操作系统,如果是XP,则需要打个.net framewo ...
- [KOJ95603]全球奥运
[COJ95603]全球奥运 试题描述 一个环形的图中有N个城市,奥运会重要项目就是传递圣火,每个城市有A[i]个圣火,每个城市可以向它相邻的城市传递圣火(其中1号城市可以传递圣火到N号城市或2号城市 ...
- 剑指Offer 整数中1出现的次数(从1到n整数中1出现的次数)
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- git 教程(12)--分支管理
分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没啥影响.不过,在某个时间点,两个平行宇宙合并 ...
- Codeforces 727 F. Polycarp's problems
Description 有一个长度为 \(n\) 有正负权值的序列,你一开始有一个值,每次到一个权值就加上,最少需要删掉多少数值才能到序列末尾.\(n \leqslant 750,m \leqslan ...