forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_164.java
139 lines (121 loc) · 4.35 KB
/
_164.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.fishercoder.solutions;
import java.util.Arrays;
/**
* 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.
*/
public class _164 {
//brute force
public int maximumGap(int[] nums) {
if (nums.length < 2) {
return 0;
}
Arrays.sort(nums);
int max = Integer.MIN_VALUE;
for (int i = 1; i < nums.length; ) {
while (i < nums.length && nums[i] == nums[i - 1]) {
i++;
}
if (i == nums.length) {
i--;
max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max;
break;
} else {
max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max;
}
if (nums[i] != nums[i - 1]) {
i++;
}
}
return max;
}
//http://www.programcreek.com/2014/03/leetcode-maximum-gap-java/
class Bucket {
int min = -1;
int max = -1;
public Bucket() {
this.min = -1;
this.max = -1;
}
}
//compute interval and multiply by interval to get the index
public int maximumGap_from_programcreek_1(int[] nums) {
if (nums == null || nums.length < 2) {
return 0;
}
int maxNum = nums[0];
int minNum = nums[0];
for (int i = 0; i < nums.length; i++) {
maxNum = Math.max(maxNum, nums[i]);
minNum = Math.min(minNum, nums[i]);
}
//initialize bucket array
Bucket[] buckets = new Bucket[nums.length + 1];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new Bucket();
}
double interval = (double) nums.length / (maxNum - minNum);
//distribute the array to different buckets
for (int i = 0; i < nums.length; i++) {
int index = (int) ((nums[i] - minNum) * interval);
if (buckets[index].min == -1) {
buckets[index].min = nums[i];
buckets[index].max = nums[i];
} else {
buckets[index].min = Math.min(nums[i], buckets[index].min);
buckets[index].max = Math.max(nums[i], buckets[index].max);
}
}
//scan through the bucket array to find the maximal gap
int result = 0;
int prev = buckets[0].max;
for (int i = 1; i < buckets.length; i++) {
if (buckets[i].min != -1) {
result = Math.max(result, buckets[i].min - prev);
prev = buckets[i].max;
}
}
return result;
}
//compute gap and divide by gap to get the index
public int maximumGap_from_programcreek_2(int[] nums) {
if (nums == null || nums.length < 2) {
return 0;
}
int maxNum = nums[0];
int minNum = nums[0];
for (int i = 0; i < nums.length; i++) {
maxNum = Math.max(maxNum, nums[i]);
minNum = Math.min(minNum, nums[i]);
}
//initialize bucket array
Bucket[] buckets = new Bucket[nums.length + 1];
for (int i = 0; i < buckets.length; i++) {
buckets[i] = new Bucket();
}
double gap = (double) (maxNum - minNum) / (nums.length - 1);
//distribute the array to different buckets
for (int i = 0; i < nums.length; i++) {
int index = (int) ((nums[i] - minNum) / gap);
if (buckets[index].min == -1) {
buckets[index].min = nums[i];
buckets[index].max = nums[i];
} else {
buckets[index].min = Math.min(nums[i], buckets[index].min);
buckets[index].max = Math.max(nums[i], buckets[index].max);
}
}
//scan through the bucket array to find the maximal gap
int result = 0;
int prev = buckets[0].max;
for (int i = 1; i < buckets.length; i++) {
if (buckets[i].min != -1) {
result = Math.max(result, buckets[i].min - prev);
prev = buckets[i].max;
}
}
return result;
}
}