8000 refactor 539 · FRANDAVID/Leetcode-3@f2b016d · GitHub
[go: up one dir, main page]

Skip to content

Commit f2b016d

Browse files
refactor 539
1 parent 9ac4c91 commit f2b016d

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

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

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

55
/**
6-
* Given a list of 24-hour clock time points in "Hour:Minutes" format, find the minimum minutes difference between any two time points in the list.
6+
* 539. Minimum Time Difference
7+
*
8+
* Given a list of 24-hour clock time points in "Hour:Minutes" format,
9+
* find the minimum minutes difference between any two time points in the list.
710
811
Example 1:
912
Input: ["23:59","00:00"]
@@ -15,37 +18,40 @@
1518
*/
1619
public class _539 {
1720

18-
public int findMinDifference(List<String> timePoints) {
19-
// there are in total 24*60 = 1440 possible time points
20-
final int ALL_POSSIBLE_TIMEPOINTS = 1440;
21-
boolean[] allTimePoints = new boolean[ALL_POSSIBLE_TIMEPOINTS];
22-
for (String eachTime : timePoints) {
23-
String[] timeParts = eachTime.split(":");
24-
int hour = Integer.valueOf(timeParts[0]);
25-
int minute = Integer.valueOf(timeParts[1]);
26-
int value = hour * 60 + minute;
27-
if (allTimePoints[value]) {
28-
return 0;
21+
public static class Soluiton1 {
22+
23+
public int findMinDifference(List<String> timePoints) {
24+
/**there are in total 24*60 = 1440 possible time points*/
25+
final int ALL_POSSIBLE_TIMEPOINTS = 1440;
26+
boolean[] allTimePoints = new boolean[ALL_POSSIBLE_TIMEPOINTS];
27+
for (String eachTime : timePoints) {
28+
String[] timeParts = eachTime.split(":");
29+
int hour = Integer.valueOf(timeParts[0]);
30+
int minute = Integer.valueOf(timeParts[1]);
31+
int value = hour * 60 + minute;
32+
if (allTimePoints[value]) {
33+
return 0;
34+
}
35+
allTimePoints[value] = true;
2936
}
30-
allTimePoints[value] = true;
31-
}
3237

33-
int min = Integer.MAX_VALUE;
34-
int prev = 0;
35-
int first = Integer.MAX_VALUE;
36-
int last = Integer.MIN_VALUE;
37-
for (int i = 0; i < ALL_POSSIBLE_TIMEPOINTS; i++) {
38-
if (allTimePoints[i]) {
39-
if (first != Integer.MAX_VALUE) {
40-
min = Math.min(min, i - prev);
38+
int min = Integer.MAX_VALUE;
39+
int prev = 0;
40+
int first = Integer.MAX_VALUE;
41+
int last = Integer.MIN_VALUE;
42+
for (int i = 0; i < ALL_POSSIBLE_TIMEPOINTS; i++) {
43+
if (allTimePoints[i]) {
44+
if (first != Integer.MAX_VALUE) {
45+
min = Math.min(min, i - prev);
46+
}
47+
first = Math.min(first, i);
48+
last = Math.max(last, i);
49+
prev = i;
4150
}
42-
first = Math.min(first, i);
43-
last = Math.max(last, i);
44-
prev = i;
4551
}
52+
min = Math.min(min, (ALL_POSSIBLE_TIMEPOINTS - last + first));
53+
return min;
4654
}
47-
min = Math.min(min, (ALL_POSSIBLE_TIMEPOINTS - last + first));
48-
return min;
4955
}
5056

5157
}

src/test/java/com/fishercoder/_539Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
import static junit.framework.Assert.assertEquals;
1313

1414
public class _539Test {
15-
private static _539 test;
15+
private static _539.Soluiton1 soluiton1;
1616
private static int expected;
1717
private static int actual;
1818
private static List<String> timePoints;
1919

2020
@BeforeClass
2121
public static void setup() {
22-
test = new _539();
22+
soluiton1 = new _539.Soluiton1();
2323
}
2424

2525
@Before
@@ -32,15 +32,15 @@ public void setupForEachTest() {
3232
public void test1() {
3333
timePoints = new ArrayList<>(Arrays.asList("23:59", "00:00"));
3434
expected = 1;
35-
actual = test.findMinDifference(timePoints);
35+
actual = soluiton1.findMinDifference(timePoints);
3636
assertEquals(expected, actual);
3737
}
3838

3939
@Test
4040
public void test2() {
4141
timePoints = new ArrayList<>(Arrays.asList("23:59", "00:00", "01:20"));
4242
expected = 1;
43-
actual = test.findMinDifference(timePoints);
43+
actual = soluiton1.findMinDifference(timePoints);
4444
assertEquals(expected, actual);
4545
}
4646

0 commit comments

Comments
 (0)
0