Skip to content

Commit 3407f4c

Browse files
refactor 475
1 parent e6ac336 commit 3407f4c

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Arrays;
44

55
/**
6-
* 475. _475
6+
* 475.
77
*
88
Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses.
99
@@ -29,21 +29,22 @@
2929
*/
3030
public class _475 {
3131

32-
//credit: https://discuss.leetcode.com/topic/71460/short-and-clean-java-binary-search-solution
33-
public static int findRadius(int[] houses, int[] heaters) {
34-
Arrays.sort(heaters);
35-
int radius = Integer.MIN_VALUE;
36-
for (int house : houses) {
37-
int index = Arrays.binarySearch(heaters, house);
38-
if (index < 0) {
39-
index = ~index;
32+
public static class Solution1 {
33+
//credit: https://discuss.leetcode.com/topic/71460/short-and-clean-java-binary-search-solution
34+
public int findRadius(int[] houses, int[] heaters) {
35+
Arrays.sort(heaters);
36+
int radius = Integer.MIN_VALUE;
37+
for (int house : houses) {
38+
int index = Arrays.binarySearch(heaters, house);
39+
if (index < 0) {
40+
index = ~index;
41+
}
42+
int distance1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
43+
int distance2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;
44+
45+
radius = Math.max(radius, Math.min(distance1, distance2));
4046
}
41-
int distance1 = index - 1 >= 0 ? house - heaters[index - 1] : Integer.MAX_VALUE;
42-
int distance2 = index < heaters.length ? heaters[index] - house : Integer.MAX_VALUE;
43-
44-
radius = Math.max(radius, Math.min(distance1, distance2));
47+
return radius;
4548
}
46-
return radius;
4749
}
48-
4950
}

src/test/java/com/fishercoder/_475Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
* Created by fishercoder on 4/23/17.
1111
*/
1212
public class _475Test {
13-
private static _475 test;
13+
private static _475.Solution1 test;
1414
private static int expected;
1515
private static int actual;
1616
private static int[] houses;
1717
private static int[] heaters;
1818

1919
@BeforeClass
2020
public static void setup() {
21-
test = new _475();
21+
test = new _475.Solution1();
2222
}
2323

2424
@Test

0 commit comments

Comments
 (0)