maximum-gap(经过了提示)
下面的分桶个数做的不太好,原来的解法是用的
int gap = (big - small) / vlen;
if (gap == 0) {
gap = 1;
}
下面是现在的Java解法:
package com.company; import java.util.*; class Solution {
public int maximumGap(int[] nums) {
if (nums.length < 2) {
return 0;
} // 用 Radix 排序
int small = Integer.MAX_VALUE;
int big = 0;
for (int num : nums) {
if (num < small) {
small = num;
}
if (num > big) {
big = num;
}
}
System.out.println("big is " + big + " small " + small);
int gap = (big - small - 1) / (nums.length - 1) + 1;
if (gap == 0) {
return 0;
}
int gap_num = (big - small) / gap + 1;
int[] first = new int[gap_num];
int[] second = new int[gap_num];
// [ )
System.out.println("gap is " + gap + " len is " + nums.length + "big is " + big + " small " + small);
for (int num : nums) {
int index = (num - small) / gap;
if (first[index] == 0 || num < first[index]) {
first[index] = num;
}
if (second[index] == 0 || num > second[index]) {
second[index] = num;
}
}
int ret = -1;
int last = -1;
for (int i=0; i<gap_num; i++) {
if (first[i] == 0) {
continue;
}
if (last == -1) {
last = second[i];
ret = last - first[i];
}
else {
if (first[i] - last > ret) {
ret = first[i] - last;
}
if (second[i] - first[i] > ret) {
ret = second[i] - first[i];
}
last = second[i];
} }
return ret;
}
} public class Main { public static void main(String[] args) {
System.out.println("Hello!");
Solution solution = new Solution(); int[] nums = {1,1,1,1,1,5,5,5,5,5}; int ret = solution.maximumGap(nums);
System.out.printf("Get ret: %s\n", ret); /*Iterator<List<Integer>> iterator = ret.iterator();
while (iterator.hasNext()) {
Iterator iter = iterator.next().iterator();
while (iter.hasNext()) {
System.out.printf("%d,", iter.next());
}
System.out.println();
}*/ System.out.println(); }
}
maximum-gap(经过了提示)的更多相关文章
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- [LintCode] Maximum Gap 求最大间距
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)
前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...
- Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- leetcode[164] Maximum Gap
梅西刚梅开二度,我也记一题. 在一个没排序的数组里,找出排序后的相邻数字的最大差值. 要求用线性时间和空间. 如果用nlgn的话,直接排序然后判断就可以了.so easy class Solution ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- 【LeetCode】164. Maximum Gap (2 solutions)
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序 在LeetCode中有个题目叫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
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
随机推荐
- nodejs redis
0. install redis library for node npm install redis 1.node command example > var _redis = require ...
- 复习linq
复习linq linq的英文是language integrated query.其中query的意思就是疑问或者计算机用语就是从资料库中提取信息的要求,可以理解为查询的意思.那么它翻译过来的话就是集 ...
- android 开发不能创建目录
原来代码: File tempDir = new File(path); //path 是一个参数 if (!tempDir.exists()) { try { tempDir.mkdir(); // ...
- SQLServer 语句-创建索引【转】
语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100GO /*实例*/USE 库名GOIF EXISTS (SELECT * ...
- hadoop历史服务器配置问题
作者:sdjnzqr 出处:http://www.cnblogs.com/sdjnzqr/ 版权:本文版权归作者和博客园共有 转载:欢迎转载,但未经作者同意,必须保留此段声明:必须在文章中给出原文连接 ...
- asynDBCenter(修改)
asynDBCenter加入数据库心跳,其实是没有找到更好的方法,看看和以前有什么不同 mongo数据库重练,暂时没有找到好办法,只能这样定时访问 bool asynDBCenter::init(bo ...
- 为什么乱码:<meta http-equiv="content-type">前的非ANSI字符
为什么乱码:<meta http-equiv="content-type">前的非ANSI字符 浏览器检测网页字符集的默认顺序 浏览器的网页字符集检测顺序通常是: ch ...
- POJ 3421
X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5111 Accepted: 1622 D ...
- IOS 视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途
iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...
- 使用git代替FTP部署代码到服务器的例子
这篇文章主要介绍了使用git代替FTP部署代码到服务器的例子,这种方法可以节省流量.节省时间,需要的朋友可以参考下 本地开发完成后,通常会在服务器上部署,有人会使用ftp,有人会使用scp, ftp和 ...