Skip to content

Commit dee9cc4

Browse files
committed
Maximum Gap: AC
1 parent c1079a0 commit dee9cc4

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

LeetCodePrj/Java/leetcode/hard/page1/MaximumGap.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@ public int maximumGap(int[] nums) {
1717
int bucketSize = Math.max(1, (maxi - mini) / ((int) nums.length - 1));
1818
int bucketNum = (maxi - mini) / bucketSize + 1;
1919

20-
Vector<Bucket> buckets = new Vector<>(bucketSize);
20+
Vector<Bucket> buckets = new Vector<>();
21+
buckets.setSize(bucketNum);
2122

2223
for (int num : nums) {
2324
int bukcetIdx = (num - mini) / bucketSize;
24-
Bucket bucket = new Bucket();
25-
bucket.used = true;
25+
Bucket bucket = buckets.get(bukcetIdx);
26+
if (bucket == null) bucket = new Bucket();
2627
bucket.minVal = Math.min(num, bucket.minVal);
2728
bucket.maxVal = Math.max(num, bucket.maxVal);
29+
buckets.set(bukcetIdx, bucket);
2830
}
2931

3032
int prevBuceketMax = mini, maxGap = 0;
3133
for (Bucket bucket : buckets) {
32-
if (!bucket.used) {
34+
if (bucket == null) {
3335
continue;
3436
}
3537

@@ -41,7 +43,6 @@ public int maximumGap(int[] nums) {
4143
}
4244

4345
class Bucket {
44-
boolean used = false;
4546
int minVal = Integer.MAX_VALUE;
4647
int maxVal = Integer.MIN_VALUE;
4748
}

0 commit comments

Comments
 (0)