File tree Expand file tree Collapse file tree
LeetCodePrj/Java/leetcode/hard/page1 Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments