【leetcode】Maximum Gap
Maximum Gap
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.
Credits:
Special thanks to @porker2008 for adding this problem and creating all test cases.
class Solution {
public:
int maximumGap(vector<int> &num) { if(num.size()<)
return ; sort(num.begin(),num.end());
int max=; for(int i=;i<num.size()-;i++)
{
if(num[i+]-num[i]>max)
max=num[i+]-num[i];
}
return max;
}
};
class Solution {
public:
int maximumGap(vector<int> &num) { if(num.size()<) return ;
if(num.size()==) return abs(num[]-num[]); int n=num.size();
int min,max,i; min=max=num[]; for(i=;i<num.size();i++)
{
if(min>num[i]) min=num[i];
if(max<num[i]) max=num[i];
} // 找到区间间隔
//注意此处,也可以写成(max-min)/n+1,此时就不需要num.size()==2的边界条件了
int dis=(max-min)/(n-)+; vector<vector<int> > bucket((max-min)/dis+); for(i=;i<n;i++)
{
int x=num[i];
int index=(x-min)/dis;
//把元素放入不同的区间中
if(bucket[index].empty())
{
bucket[index].reserve();
bucket[index].push_back(x);
bucket[index].push_back(x);
}
else
{
if(bucket[index][]>x) bucket[index][]=x;
if(bucket[index][]<x) bucket[index][]=x;
}
} int pre=;
int gap=; //在相邻的区间中(区间内有元素的相邻区间)寻找最大的gap
for(i=;i<bucket.size();i++)
{
if(bucket[i].empty()) continue; int tmp=bucket[i][]-bucket[pre][];
if(gap<tmp) gap=tmp;
pre=i;
}
return gap;
}
};
【leetcode】Maximum Gap的更多相关文章
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【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 ...
随机推荐
- Python 线程、进程和协程
python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费时间,所以我们直接学习threading 就可以了. ...
- Python之路【第四篇补充】:面向对象初识和总结回顾
面向过程的编程 面向过程:根据业务逻辑从上到下写垒代码! 例子: 需求一.有一个程序需要做身份认证: 用户名有个字典: #定义一个用户名信息字典 user_info = { "zhangsa ...
- css书写规则总结
1. JavaScript钩子使用的class不能是css class,要加j或j-前缀 2. 选择器 2.1 css选择器尽量简短,层级要少,最好是1-2层 例如:.nav{} 优于 ul.nav{ ...
- [c#]exchange回复,全部回复,转发所遇到的问题
摘要 场景: 用户B向A用户发送了一封邮件. 用户A答复邮件时,会默认将B作为接收人. 问题: 在用exchange的回复,全部回复,转发(Reply和Foward方法)邮件的时候,需求是用户可以删除 ...
- CF456B Fedya and Maths 找规律
http://codeforces.com/contest/456/problem/B CF#260 div2 B Fedya and Maths Codeforces Round #260 B. F ...
- Eclipse闪退无法打开的解决方法
使用Eclipse过程中但是有时会出现打不开闪退的情况,这是为什么呢,遇到这种情况怎么解决.东坡小编通过查找资料,发现如下方法可以解决eclipse打不开闪退,具体操作如下: Eclipse打不开闪退 ...
- 使用MVVM框架avalon.js实现一个简易日历
最近在做公司内部的运营管理系统,因为与日历密切相关,同时无需触发条件直接显示在页面上,所以针对这样的功能场景,我就用avalon快速实现了一个简易日历,毕竟也是第一次造日历这种轮子,所以这里记录下我当 ...
- [设计模式] Javascript 之 观察者模式
观察者模式:定议 定义对象间的一种一对多的关系,当一个对象状态改变时 (一般称为被观察者),依赖于该对象的对象被通知,并更新; 观察者模式:说明 1. 观察者模式是行为模式,也被称为:发布-订阅模式. ...
- Ruby学习之module
我们可以认为module是一个专门存放一系列方法和常量的工具箱. module和class非常像, 只是module不能创建实例也不能有子类, 它们仅仅能存放东西. 例如: module Circle ...
- HDOJ 3652 B-number
B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...