Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2a885ea

Browse files
refactor 164
1 parent cb5cd84 commit 2a885ea

File tree

2 files changed

+67
-185
lines changed

2 files changed

+67
-185
lines changed

src/main/java/com/fishercoder/solutions/_164.java

Lines changed: 42 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -3,137 +3,52 @@
33
import java.util.Arrays;
44

55
/**
6+
* 164. Maximum Gap
7+
*
68
* Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
7-
* Try to solve it in linear time/space.
89
* Return 0 if the array contains less than 2 elements.
9-
* You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
10+
*
11+
* Example 1:
12+
* Input: [3,6,9,1]
13+
* Output: 3
14+
* Explanation: The sorted form of the array is [1,3,6,9], either
15+
* (3,6) or (6,9) has the maximum difference 3.
16+
*
17+
* Example 2:
18+
* Input: [10]
19+
* Output: 0
20+
* Explanation: The array contains less than 2 elements, therefore return 0.
21+
*
22+
* Note:
23+
* You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
24+
* Try to solve it in linear time/space.
1025
*/
1126
public class _164 {
12-
//brute force
27+
public static class Solution1 {
28+
/** brute force solution */
1329
public int maximumGap(int[] nums) {
14-
if (nums.length < 2) {
15-
return 0;
16-
}
17-
18-
Arrays.sort(nums);
19-
int max = Integer.MIN_VALUE;
20-
for (int i = 1; i < nums.length; ) {
21-
while (i < nums.length && nums[i] == nums[i - 1]) {
22-
i++;
23-
}
24-
if (i == nums.length) {
25-
i--;
26-
max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max;
27-
break;
28-
} else {
29-
max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max;
30-
}
31-
if (nums[i] != nums[i - 1]) {
32-
i++;
33-
}
34-
}
35-
return max;
36-
}
37-
38-
39-
//http://www.programcreek.com/2014/03/leetcode-maximum-gap-java/
40-
class Bucket {
41-
int min = -1;
42-
int max = -1;
43-
44-
public Bucket() {
45-
this.min = -1;
46-
this.max = -1;
47-
}
48-
}
49-
50-
//compute interval and multiply by interval to get the index
51-
public int maximumGap_from_programcreek_1(int[] nums) {
52-
if (nums == null || nums.length < 2) {
53-
return 0;
54-
}
55-
56-
int maxNum = nums[0];
57-
int minNum = nums[0];
58-
for (int i = 0; i < nums.length; i++) {
59-
maxNum = Math.max(maxNum, nums[i]);
60-
minNum = Math.min(minNum, nums[i]);
61-
}
62-
63-
//initialize bucket array
64-
Bucket[] buckets = new Bucket[nums.length + 1];
65-
for (int i = 0; i < buckets.length; i++) {
66-
buckets[i] = new Bucket();
67-
}
68-
69-
double interval = (double) nums.length / (maxNum - minNum);
70-
//distribute the array to different buckets
71-
for (int i = 0; i < nums.length; i++) {
72-
int index = (int) ((nums[i] - minNum) * interval);
73-
if (buckets[index].min == -1) {
74-
buckets[index].min = nums[i];
75-
buckets[index].max = nums[i];
76-
} else {
77-
buckets[index].min = Math.min(nums[i], buckets[index].min);
78-
buckets[index].max = Math.max(nums[i], buckets[index].max);
79-
}
80-
}
81-
82-
//scan through the bucket array to find the maximal gap
83-
int result = 0;
84-
int prev = buckets[0].max;
85-
for (int i = 1; i < buckets.length; i++) {
86-
if (buckets[i].min != -1) {
87-
result = Math.max(result, buckets[i].min - prev);
88-
prev = buckets[i].max;
89-
}
90-
}
91-
92-
return result;
93-
}
94-
95-
//compute gap and divide by gap to get the index
96-
public int maximumGap_from_programcreek_2(int[] nums) {
97-
if (nums == null || nums.length < 2) {
98-
return 0;
99-
}
100-
101-
int maxNum = nums[0];
102-
int minNum = nums[0];
103-
for (int i = 0; i < nums.length; i++) {
104-
maxNum = Math.max(maxNum, nums[i]);
105-
minNum = Math.min(minNum, nums[i]);
106-
}
107-
108-
//initialize bucket array
109-
Bucket[] buckets = new Bucket[nums.length + 1];
110-
for (int i = 0; i < buckets.length; i++) {
111-
buckets[i] = new Bucket();
112-
}
113-
114-
double gap = (double) (maxNum - minNum) / (nums.length - 1);
115-
//distribute the array to different buckets
116-
for (int i = 0; i < nums.length; i++) {
117-
int index = (int) ((nums[i] - minNum) / gap);
118-
if (buckets[index].min == -1) {
119-
buckets[index].min = nums[i];
120-
buckets[index].max = nums[i];
121-
} else {
122-
buckets[index].min = Math.min(nums[i], buckets[index].min);
123-
buckets[index].max = Math.max(nums[i], buckets[index].max);
124-
}
125-
}
126-
127-
//scan through the bucket array to find the maximal gap
128-
int result = 0;
129-
int prev = buckets[0].max;
130-
for (int i = 1; i < buckets.length; i++) {
131-
if (buckets[i].min != -1) {
132-
result = Math.max(result, buckets[i].min - prev);
133-
prev = buckets[i].max;
134-
}
135-
}
136-
137-
return result;
30+
if (nums.length < 2) {
31+
return 0;
32+
}
33+
34+
Arrays.sort(nums);
35+
int max = Integer.MIN_VALUE;
36+
for (int i = 1; i < nums.length; ) {
37+
while (i < nums.length && nums[i] == nums[i - 1]) {
38+
i++;
39+
}
40+
if (i == nums.length) {
41+
i--;
42+
max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max;
43+
break;
44+
} else {
45+
max = (nums[i] - nums[i - 1] > max) ? nums[i] - nums[i - 1] : max;
46+
}
47+
if (nums[i] != nums[i - 1]) {
48+
i++;
49+
}
50+
}
51+
return max;
13852
}
53+
}
13954
}
Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,35 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._164;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

87
import static junit.framework.Assert.assertEquals;
98

109
public class _164Test {
11-
private static _164 test;
12-
private static int expected;
13-
private static int actual;
14-
private static int[] nums;
15-
16-
@BeforeClass
17-
public static void setup() {
18-
test = new _164();
19-
}
20-
21-
@Before
22-
public void setupForEachTest() {
23-
expected = 0;
24-
actual = 0;
25-
}
26-
27-
@Test
28-
public void test1() {
29-
nums = new int[]{};
30-
expected = 0;
31-
actual = test.maximumGap(nums);
32-
assertEquals(expected, actual);
33-
34-
actual = test.maximumGap_from_programcreek_1(nums);
35-
assertEquals(expected, actual);
36-
37-
actual = test.maximumGap_from_programcreek_2(nums);
38-
assertEquals(expected, actual);
39-
}
40-
41-
@Test
42-
public void test2() {
43-
nums = new int[]{1, 3, 6, 5};
44-
expected = 2;
45-
actual = test.maximumGap(nums);
46-
assertEquals(expected, actual);
47-
48-
actual = test.maximumGap_from_programcreek_1(nums);
49-
assertEquals(expected, actual);
50-
51-
actual = test.maximumGap_from_programcreek_2(nums);
52-
assertEquals(expected, actual);
53-
}
54-
55-
@Test
56-
public void test3() {
57-
nums = new int[]{1, 100000};
58-
expected = 99999;
59-
actual = test.maximumGap(nums);
60-
assertEquals(expected, actual);
61-
62-
actual = test.maximumGap_from_programcreek_1(nums);
63-
assertEquals(expected, actual);
64-
65-
actual = test.maximumGap_from_programcreek_2(nums);
66-
assertEquals(expected, actual);
67-
}
10+
private static _164.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _164.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[] {};
21+
assertEquals(0, solution1.maximumGap(nums));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
nums = new int[] {1, 3, 6, 5};
27+
assertEquals(2, solution1.maximumGap(nums));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
nums = new int[] {1, 100000};
33+
assertEquals(99999, solution1.maximumGap(nums));
34+
}
6835
}

0 commit comments

Comments
 (0)