forked from zfman/AlgorithmCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxWorkinghourGap.java
More file actions
43 lines (39 loc) · 1.18 KB
/
Copy pathMaxWorkinghourGap.java
File metadata and controls
43 lines (39 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ali2019;
import java.util.*;
/**
* 阿里2018秋招客户端开发工程师编程测试
*
* 题目是关于工时预估的,
* 我这里将题目简化一下:
*
* 给定一个数组,如果数组只有一个元素,返回0
* 对数组排序,然后计算相邻的差的最大值
*
* 要求时间复杂度为O(n),空间复杂度也有限制(具体多少记不清了)
*
* @author 刘壮飞
* https://github.com/zfman.
* https://blog.csdn.net/lzhuangfei.
*/
public class MaxWorkinghourGap {
/** 请完成下面这个函数,实现题目要求的功能 **/
/** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^ **/
static int maxWorkinghourGap(int[] workinghours) {
if(workinghours.length<=1) return 0;
Arrays.sort(workinghours);
int max=0,tmp=0;
for(int i=1;i<workinghours.length;i++){
tmp=workinghours[i]-workinghours[i-1];
if(tmp>max) max=tmp;
}
return max;
}
public static void main(String[] args){
int[] a={
1,3,15,10,9
};
//output:6
int r=maxWorkinghourGap(a);
System.out.println(r);
}
}