-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumTimeDifference.java
More file actions
47 lines (39 loc) · 1.36 KB
/
MinimumTimeDifference.java
File metadata and controls
47 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://leetcode.com/problems/minimum-time-difference/description/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MinimumTimeDifference {
public static void main(String[] args) {
String[] timeStrings = new String[]{"00:00","23:59","01:25", "02:45", "03:15", "18:10"};
ArrayList<String> times = new ArrayList<>();
for (String t : timeStrings) {
times.add(t);
}
System.out.println(new MinimumTimeDifference().findMinDifference(times));
}
public int findMinDifference(List<String> timePoints) {
// convert times to minutes
int[] minutes = new int[timePoints.size()];
int k = 0;
int h, m;
for (String t : timePoints) {
h = Integer.parseInt(t.substring(0,2));
m = Integer.parseInt(t.substring(3));
minutes[k++] = h*60+m;
}
Arrays.sort(minutes);
// compare first to last
int min = minutes[minutes.length-1] - minutes[0];
if (min > 12*60)
min = 24*60 - min;
int diff = 0;
for (int i = 0; i < minutes.length-1; i++) {
diff = minutes[i+1]-minutes[i];
if (diff > 12*60)
diff = 24*60-min;
if (diff < min)
min = diff;
}
return min;
}
}